0% found this document useful (0 votes)
9 views39 pages

Assignment 3

Uploaded by

alina.raza2017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views39 pages

Assignment 3

Uploaded by

alina.raza2017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Subject PROGRAMMING

FUNDAMENTALS

Name ALINA RAZA

Reg.no. FA23-BSE-015

Assignment 3

Date 11-5-24

Moderator:
SIR MUZAFFAR IQBAL
Fa23-bse-015

ASSIGNMENT 3:

QUESTION 1:
Write a program that reads in ten numbers and displays the number of
distinct numbers and the distinct numbers separated by exactly one space
(i.e., if a number appears multiple times, it is displayed only once). (Hint:
Read a number and store it to an array if it is new. If the number is
already in the array, ignore it.) After the input, the array contains the
distinct numbers.

CODE:
package assignment3;

import java.util.Scanner;

public class Ques1 {

public static void main(String[] args) {

int[] n=new int[10];

int counter=0;

Scanner input= new Scanner(System.in);

System.out.println("enter 10 numbers: ");

for(int i=0;i<10;i++){

int num=input.nextInt();

if(!contain(n,num)){

n[counter]=num;

2
Fa23-bse-015

counter++;

System.out.println("the number of distinct numbers: " + counter);

System.out.println("the distinct numbers are: ");

for(int i=0;i<counter;i++){

System.out.print(n[i] + " ");

public static boolean contain(int[] array,int num){

for (int i=0;i<array.length;i++){

if (array[i]==num){

return true;

return false;

3
Fa23-bse-015

OUTPUT:

4
Fa23-bse-015

QUESTION 2:
Write a method that returns a new array by eliminating the duplicate
values in the array using the following method header:

public static int[] eliminateDuplicates(int[] list)

Write a test program that reads in ten integers, invokes the method, and
displays the result.

CODE:
package assignment3;

import java.util.Scanner;

public class Ques2 {

public static void main(String[] args) {

int[] n=new int[10];

int counter=0;

Scanner input= new Scanner(System.in);

System.out.print("enter the 10 numbers: ");

for(int i=0;i<10;i++){

int num=input.nextInt();

if(!contains(n,num)){

n[counter]=num;

counter++;

5
Fa23-bse-015

System.out.print("the distinct numbers are: ");

for(int i=0;i<counter;i++){

System.out.print(n[i] + " ");

public static boolean contains(int[] array,int num){

for(int i=0;i<array.length;i++){

if(array[i]==num){

return true;

return false;

6
Fa23-bse-015

OUTPUT:

7
Fa23-bse-015

QUESTION 3:
Write the following method that returns true if the list is already sorted in
increasing order.

public static boolean isSorted(int[] list)

Write a test program that prompts the user to enter a list and displays
whether the list is sorted or not. If the list is not in sorted order then sort
the list and display.

CODE:
import java.util.Arrays;

import java.util.Scanner;

public class Ques3 {

public static void main(String[] args) {

int[] n=new int[10];

Scanner input=new Scanner(System.in);

System.out.print("enter the list of ten numbers: ");

for(int i=0;i<n.length;i++){

n[i]=input.nextInt();

if(!isSorted(n)){

Arrays.sort(n);

System.out.println("your list is not sorted, the sorted list is: ");

display(n);

8
Fa23-bse-015

else {

System.out.println("your list is already sorted");

public static boolean isSorted(int[] list){

for(int i=0;i<list.length-1;i++){

for(int j=i+1;j<list.length;j++){

if(list[i]>list[j]){

return false;

return true;

public static void display(int[] array){

for(int x:array)

System.out.print(x + " ");

9
Fa23-bse-015

OUTPUT:

10
Fa23-bse-015

QUESTION 4:
Write the following method that tests whether the array has four
consecutive numbers with the same value.

public static boolean isConsecutiveFour(int[] values)

Write a test program that prompts the user to enter a series of integers
and dis plays if the series contains four consecutive numbers with the
same value. Your program should first prompt the user to enter the input
size—i.e., the number of values in the series

CODE:
package assignment2;

import java.util.Scanner;

public class Ques4 {

public static void main(String[] args){

Scanner input=new Scanner(System.in);

System.out.print("enter the size of the list: ");

int size=input.nextInt();

int[] n=new int[size];

System.out.print("enter " + size+ " values of the list ");

for(int i=0;i<n.length;i++){

n[i]=input.nextInt();

if(size>=4){

if(isConsecutiveFour(n)){

11
Fa23-bse-015

System.out.println("the list has consecutive fours");

else{

System.out.println("the list has no consecutive fours");

else

System.out.print("you haven't entered a valid list size.");

public static boolean isConsecutiveFour(int[] values){

for(int i=0;i<values.length;i++){

if((values[i]==values[i+1]) && (values[i+1]==values[i+2]) &&


(values[i+2]==values[i+3]) && (values[i+3]==values[i])){

return true;

return false;

OUTPUT:

12
Fa23-bse-015

QUESTION 5:
Write the following method that merges two sorted lists into a new sorted
list.

Implement the method in a way that takes at most list1.length + list2.


Length comparisons.

Write a test program that prompts the user to enter two sorted lists and
displays the merged list.

Note that the first number in the input indicates the number of the
elements in the list.

This number is not part of the list.

CODE:
package assignment3;

13
Fa23-bse-015

import java.util.Arrays;

import java.util.Scanner;

public class Ques5{

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.println("enter the size of list 1: ");

int s1=input.nextInt();

System.out.println("enter list 1: ");

int[] n1=new int[s1];

for(int i=0;i<n1.length;i++){

n1[i]=input.nextInt();

System.out.println("enter the size of list 2: ");

int s2=input.nextInt();

System.out.println("enter list2: ");

int[] n2=new int[s2];

for(int i=0;i<n2.length;i++){

n2[i]=input.nextInt();

System.out.print("the merged list is: " );

14
Fa23-bse-015

int[] merged=isSortedMerged(n1,n2);

int[] m=sortedList(merged);

for(int x:m){

System.out.print(x + " ");

public static int[] isSortedMerged(int[] arr1,int[] arr2){

int[] mergedList = new int[arr1.length + arr2.length];

int i = 0, j = 0, k = 0;

while (i < arr1.length && j < arr2.length) {

if (arr1[i] < arr2[j]) {

mergedList[k++] = arr1[i++];

} else {

mergedList[k++] = arr2[j++];

while (i < arr1.length) {

mergedList[k++] = arr1[i++];

15
Fa23-bse-015

while (j < arr2.length) {

mergedList[k++] = arr2[j++];

mergedList=sortedList(mergedList);

return mergedList;

public static int[] sortedList(int[] arr){

for (int i = 0; i < arr.length-1; i++) {

for(int j=i+1;j<arr.length;j++){

if(arr[i]>arr[j]){

int temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

16
Fa23-bse-015

return arr;

OUTPUT:

QUESTION 6:

Write a JAVA Program to Multiply Two Matrix Using Multi-Dimensional


Arrays. This program takes two matrices of order r1*c1 and r2*c2
respectively. Then, the program multiplies these two matrices (if possible)
and displays it on the screen.

CODE:
package assignment3;

import java.util.Scanner;

17
Fa23-bse-015

public class Ques6 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter number of rows for the matrix1: ");

int r1 = input.nextInt();

System.out.print("Enter number of columns for the matrix2: ");

int c1 = input.nextInt();

System.out.print("Enter number of rows for the matrix2: ");

int r2 = input.nextInt();

System.out.print("Enter number of columns for the matrix2: ");

int c2 = input.nextInt();

if (c1 != r2) {

System.out.println("Matrix multiplication is not possible.");

} else {

int[][] matrix1 = new int[r1][c1];

System.out.println("Enter elements for matrix1:");

enterMatrixElements(input, matrix1);

int[][] matrix2 = new int[r2][c2];

System.out.println("Enter elements for matrix2:");

18
Fa23-bse-015

enterMatrixElements(input, matrix2);

int[][] result = multiplyMatrices(matrix1, matrix2);

System.out.println("Result of matrix multiplication:");

displayMatrix(result);

input.close();

public static void enterMatrixElements(Scanner input, int[][] matrix) {

for (int i = 0; i < matrix.length; i++) {

for (int j = 0; j < matrix[0].length; j++) {

System.out.print("Enter element at position [" + i + "][" + j + "]: ");

matrix[i][j] = input.nextInt();

public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {

int r1 = matrix1.length;

int c1 = matrix1[0].length;

19
Fa23-bse-015

int c2 = matrix2[0].length;

int[][] result = new int[r1][c2];

for (int i = 0; i < r1; i++) {

for (int j = 0; j < c2; j++) {

for (int k = 0; k < c1; k++) {

result[i][j] += matrix1[i][k] * matrix2[k][j];

return result;

public static void displayMatrix(int[][] matrix) {

for (int[] row : matrix) {

for (int num : row) {

System.out.print(num + "\t");

System.out.println();

20
Fa23-bse-015

OUTPUT:

21
Fa23-bse-015

QUESTION 7:
Write a program that prompts the user to enter the length of a square
matrix, randomly fills in 0s and 1s into the matrix, prints the matrix, and
finds the rows, columns, and diagonals with all 0s or 1s.

CODE:
package assignment3;

import java.util.Random;

import java.util.Scanner;

public class Ques7 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

Random random = new Random();

System.out.print("Enter the length of the square matrix: ");

int length = input.nextInt();

int[][] matrix = new int[length][length];

for (int i = 0; i < length; i++) {

for (int j = 0; j < length; j++) {

matrix[i][j] = random.nextInt(2);

22
Fa23-bse-015

System.out.println("Generated square matrix:");

display(matrix);

checkRowsColumnsDiagonals(matrix);

public static void display(int[][] matrix) {

for (int[] row : matrix) {

for (int num : row) {

System.out.print(num + " ");

System.out.println();

public static void checkRowsColumnsDiagonals(int[][] matrix) {

int length = matrix.length;

for (int i = 0; i < length; i++) {

boolean allZeros = true;

boolean allOnes = true;

for (int j = 0; j < length; j++) {

if (matrix[i][j] != 0) {

allZeros = false;

23
Fa23-bse-015

if (matrix[i][j] != 1) {

allOnes = false;

if (allZeros) {

System.out.println("All 0s in row " + (i + 1));

if (allOnes) {

System.out.println("All 1s in row " + (i + 1));

for (int j = 0; j < length; j++) {

boolean allZeros = true;

boolean allOnes = true;

for (int i = 0; i < length; i++) {

if (matrix[i][j] != 0) {

allZeros = false;

if (matrix[i][j] != 1) {

allOnes = false;

24
Fa23-bse-015

if (allZeros) {

System.out.println("All 0s in column " + (j + 1));

if (allOnes) {

System.out.println("All 1s in column " + (j + 1));

boolean allZerosDiagonal1 = true;

boolean allOnesDiagonal1 = true;

boolean allZerosDiagonal2 = true;

boolean allOnesDiagonal2 = true;

for (int i = 0; i < length; i++) {

if (matrix[i][i] != 0) {

allZerosDiagonal1 = false;

if (matrix[i][i] != 1) {

allOnesDiagonal1 = false;

if (matrix[i][length - i - 1] != 0) {

25
Fa23-bse-015

allZerosDiagonal2 = false;

if (matrix[i][length - i - 1] != 1) {

allOnesDiagonal2 = false;

if (allZerosDiagonal1) {

System.out.println("All 0s in diagonal 1");

if (allOnesDiagonal1) {

System.out.println("All 1s in diagonal 1");

if (allZerosDiagonal2) {

System.out.println("All 0s in diagonal 2");

if (allOnesDiagonal2) {

System.out.println("All 1s in diagonal 2");

26
Fa23-bse-015

OUTPUT:

QUESTION 8:
Write a Java program to accept roll numbers, names and marks of 10
students. Sort this data on the basis of marks in descending order such
that data of student having maximum marks should come at the top and
the data of student having minimum marks should come at the bottom.

27
Fa23-bse-015

CODE:
package assignment3;

import java.util.Scanner;

public class Ques8 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int[] rollNum=new int[5];

String[] names = new String[5];

double[] marks=new double[5];

for(int i=0;i<5;i++){

System.out.print("enter roll number: ");

rollNum[i]=input.nextInt();

System.out.print("enter name: ");

names[i]=input.next();

System.out.print("enter marks: ");

marks[i]=input.nextDouble();

descendingSort(rollNum,names,marks);

System.out.println("the sorted data is: ");

System.out.println("Roll Number \tName \tMarks");

for(int i=0;i<5;i++){

28
Fa23-bse-015

System.out.println(rollNum[i] + "\t\t" + names[i] + "\t\t" + marks[i]);

public static void descendingSort(int[] x,String[] y,double[] z){

for(int i=0;i<4;i++){

for(int j=i+1;j<5;j++){

if(z[i]<z[j]){

double temp=z[i];

z[i]=z[j];

z[j]=temp;

int temp1=x[i];

x[i]=x[j];

x[j]=temp1;

String temp2=y[i];

y[i]=y[j];

y[j]=temp2;

29
Fa23-bse-015

OUTPUT:

30
Fa23-bse-015

QUESTION 9:
Write a Menu Driven JAVA program that creates one-dimensional array
arr[] and initialize it with user. The program should do following Tasks
using Menu, the menu operations are implemented using methods:

1. Write a method count(), that counts the occurrences of x (a number) in


arr[].

2. Write a method partition(), that take the first element of the array x and
put x in a position such that all smaller elements (smaller than x) are
before x, and put all greater elements (greater than x) after x.

3. Write a method duplicates(),which calculate the frequencies of all the


elements and display them.

4. Write a method circular(),which replace every element of the array by


the sum of next two consecutive elements in a circular manner i.e.

arr[0] = arr[1] + arr[2], arr[1] = arr[2] + arr[3], … arr[n – 1] = arr[0] +


arr[1].

5. Write a method shiftCircular(), which shifts an array circularly left by


two positions. Thus, if p[0] = 15, p[1]= 30, p[2] = 28, p[3]= 19 and p[4] = 61
then after the shift p[0] = 28, p[1] = 19, p[2] = 61, p[3] = 15 and p[4] = 30.

CODE:
package assignment3;

import java.util.Scanner;

public class Ques9 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("enter the size of an array ");

31
Fa23-bse-015

int size=input.nextInt();

System.out.println("Menu \n1.Counting the occurences of a number ");

System.out.println("2.Partion the array \n3.Calculate frequency of duplicates \


n4.Circular replacement");

System.out.println("5.Circular shifting of elements ");

System.out.println("enter the option from the menu: ");

int option=input.nextInt();

int[] array=new int[size];

System.out.println("enter the elements of an array: ");

for(int i=0;i<size;i++){

array[i]=input.nextInt();

switch(option){

case 1:{

System.out.print("enter the number: ");

int num=input.nextInt();

System.out.print(num + " occurs " + count(array,num) + " times");

break;

case 2:{

System.out.print("enter the pivot point");

32
Fa23-bse-015

int pivot=input.nextInt();

partition(array,pivot);

break;

case 3:{

duplicates(array);

break;

case 4:{

circular(array);

break;

case 5:{

shiftCircular(array);

break;

default:

System.out.println("invalid choice");

public static int count(int[] arr,int num){

33
Fa23-bse-015

int counter=0;

for(int i=0;i<arr.length;i++){

if(arr[i]==num){

counter++;

return counter;

public static void partition(int[]arr,int pivot){

int x = arr[0];

int i = 1;

for (int j = 1; j < arr.length; j++) {

if (arr[j] <= x) {

i++;

swap(arr, i - 1, j);

swap(arr, 0, i - 1);

System.out.println("Partitioned array: " + java.util.Arrays.toString(arr));

34
Fa23-bse-015

public static void swap(int[] arr, int i, int j) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

public static void duplicates(int[] arr) {

int[] freq = new int[arr.length];

for (int i = 0; i < arr.length; i++) {

freq[i] = 1;

for (int j = i + 1; j < arr.length; j++) {

if (arr[i] == arr[j]) {

freq[i]++;

arr[j] = Integer.MAX_VALUE;

System.out.println("Frequencies of elements:");

for (int i = 0; i < arr.length; i++) {

if (arr[i] != Integer.MAX_VALUE) {

System.out.println(arr[i] + ": " + freq[i]);

35
Fa23-bse-015

public static void circular(int[] arr) {

int temp1 = arr[0];

int temp2 = arr[1];

int duplicate = -1;

for (int i = 0; i < arr.length - 2; i++) {

if (arr[i] == arr[i + 2]) {

duplicate = arr[i];

arr[i] = arr[i + 2] = Integer.MIN_VALUE;

} else {

arr[i] = arr[i + 2];

arr[arr.length - 2] = temp1;

arr[arr.length - 1] = temp2;

System.out.println("Circularly replaced array: " + java.util.Arrays.toString(arr));

if (duplicate != -1) {

System.out.println("Duplicate value: " + duplicate);

public static void shiftCircular(int[] arr) {

int temp1 = arr[0];

36
Fa23-bse-015

int temp2 = arr[1];

for (int i = 0; i < arr.length - 2; i++) {

arr[i] = arr[i + 2];

arr[arr.length - 2] = temp1;

arr[arr.length - 1] = temp2;

System.out.println("Circularly shifted array: " + java.util.Arrays.toString(arr));

OUTPUT:

37
Fa23-bse-015

38
Fa23-bse-015

39

You might also like