Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

esa’s Acad

er e
T

my
St

1
I would like to express my special
thanks of gratitude to my
"Computer Science” teacher 'Sir
harvinder for his able guidance
and support in completing my
project.

I would also like to extend my


gratitude to the principal “Sister
Blaisy” for providing me with all
the facilities that was required.

2
This is to certify that Dhruv
Ratan of class 11th science has
successfully completed his
Computer Science under the
able guidance of Sir Harvinder.

This project has been made


under the norms which were
provided by CISCE council.

3
Question. 1) Write a program to check if a no. is Smith Number or not.

A Smith number is a composite number where the sum of its digits is equal to
the sum of its prime factors

Ex:-
Input: n = 4
Output: Yes
Prime factorization = 2, 2 and 2 + 2 = 4
Therefore, 4 is a smith number

Input: n = 6
Output: No
Prime factorization = 2, 3 and 2 + 3 is
not 6. Therefore, 6 is not a smith number

Input: n = 666
Output: Yes
Prime factorization = 2, 3, 3, 37 and
2 + 3 + 3 + (3 + 7) = 6 + 6 + 6 = 18
Therefore, 666 is a smith number

4
Algorithm
1) START
2) Input the no. from the user
3) Set sum1 and sum 2 to 0
4) Call function sum using variable sum1
5) Function Sum accepts the variable and stores it in a variable n
6) Initialize s=0 and r=0
7) Repeat steps 8 to 10 till n! = 0
8) Save the remainder of n divided by 10 in variable r
9) Add r in the variable s
10) Divide the number n by 10
11) Return s to main function
12) Value of snow stored in sum1
13) Function primesum is called by variable s2
14) Punction primesum accepts the no. and stores in a variable n
15) Initialize f=2, t1, t=n, d, sum =0
16) Repeat steps from 17 to 24 till t is greater than 1
17) If remainder of t divided by f is 0 repeat steps from 18 to 23 otherwise
perform the step 24
18) Store the value of f in t1
19) Repeat the steps 20 to 22 till t1 is not equal to 0
20) Store the remainder of t1 divided by 10 in variable d
21) Add the value of d in sum
22) Divide t1 by 10
23) Divide t by f
24) Increase f by 1
25) Return sum to the main function
26) Value of sum is stored in s2
27) Now in main function if s1 is equal to s2
28) Print It is a smith no.
29) Otherwise
30) Print It is not a Smith no.
31) STOP

5
Java Program
import java.util.*;
class smudge
{
int sum(int n)
{
int s=0;
while(n!=0)
{
int r=n%10;
s+=r;
n/=10;
}
return s;
}
boolean checkprime(int n)
{
int c=0;
for(int i =1;i<n;i++)
{
if(n%i == 0)
{
c++;
}
}
if(c==2)
{
return true;
}
else
{
return false;
}
}
int primesum(int n)
{
int f=2;int t1;int t=n;int d,sum=0;
while(t>1)
{
if(t%f == 0)
{
t1=f;
while(t1!=0)
{
d=t1%10;

6
sum+=d;
t1/=10;
}
t/=f;
}
else
{
f++;
}
}
return sum;
}
public void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the no.");
int n = obj.nextInt();
int s1=sum(n);
int s2=primesum(n);
if(s1==s2)
{
System.out.println("Yes it is a smith no.");
}
else
{
System.out.println("No it is not a smith no.");
}
System.out.println(s1);
System.out.println(s2);
}
}

7
Question. 2) Write a program to check if a no. is Circular Prime or not
and also print all the combinations of the no.

A prime number is said to be a circular prime if after any cyclic


permutations of the digits, it remains a prime.
Examples:

Input : n = 113
Output : Yes
All cyclic permutations of (113 311 and 131) are prime.

Input : 1193
Output : Yes

8
Algorithm
• START
• Initialize n
• Input the number from the user which is to be checked in the variable n
• Create a Function checkprime which takes a input n
• Initialize c to 0 and i to 1
• Repeat steps 6 to 8 till I less than n
• If remainder of n divided by i is equal to 0
• Increase c by 1
• Increase i by 1
• If c is equal to 2
• Return true otherwise
• Return false
• Function is_circular_prime takes an input n
• Initialize t1 to n and count to 0
• Repeat steps 15 and 16 till t1 is not equal to 0
• Increase count by 1
• Divide t1 by 10
• Initialize num to n and p to -1
• Initialize rem to the remainder of num divided by 10
• Initialize d to the quotient of num divided by 10
• Reinitialize num to 10 to the power count-1 multiplied by rem and add d
• If the return of function checkprime called using variable num if false

9
• Initialize p to 1
• Print num
• Repeat above steps 19 to 24 till n is not equal to num
• If p is equal to -1
• Print Yes! It is a circular prime number
• Otherwise Print No It is not a circular prime number
• Call function is_circular_prime using variable n from the main function
• STOP

10
Java Program
import java.util.*;
class circular_prime
{
public void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the no.");
int n = obj.nextInt();
is_circular_prime(n);
}

boolean checkprime(int n)
{
int c =0;
for(int i =1;i<n;i++)
{
if(n%i == 0)
{
c++;
}
}
if(c==2)
{
return false;
}
else
{
return true;
}
}

void is_circular_prime(int n)
{
int t1=n;int count=0;
while(t1!=0)
{
count++;
t1/=10;
}
int num = n;int p=-1;
System.out.println("All the circular combination");
do
{
int rem = num%10;

11
int d = num/10;
num = (int) (Math.pow(10,count-1)*rem)+d;
if(!checkprime(num))
{
p=1;
}
System.out.println(num);
}while(n != num);
if(p==-1)
{
System.out.println("Yes! it is a circular prime no.");
}
else
{
System.out.println("No it is not a circluar prime no.");
}
}
}
OUTPUT
Input the no.
1193
All the circular combination
3119
9311
1931
1193
Yes! it is a circular prime no.

12
Question 3: - A positive natural number, (for e.g. 27), can be
represented as follows -
2+3+4+5+6+7
8+9+10
13+14
where every row represents a combination of consecutive
natural numbers which add up to 27.
Write a program which inputs a positive natural number N and
prints the possible consecutive number combinations, which
when added give N.

Test your program for the following data and some random data.
SAMPLE DATA
INPUT:
N-9
OUTPUT:
4+5
2+3+4

13
ALGORITHM
• START
• Define a class named "number".
• Declare a public static method named "main".
• Inside the "main" method, create a Scanner object 'sc' to read input from
the user.
• Print "Enter a number " to prompt the user for input.
• Read an integer value from the user and store it in the variable 'n'.
• Declare an integer variable 'sum' and 'j' and initialize them to 0.
• Begain a for loop with 'i' initialized to 1 and the condition 'i' less than 'n'.
• Within the loop, set 'sum' equal to 'i' and 'j' equal to 'i + 1'.
• Begain a while loop with the condition 'sum' less than 'n'.
• Within the while loop, add 'j' to 'sum' and increment 'j' by 1.
• Exit the while loop when 'sum' becomes greater than or equal to 'n'.
• Check if 'sum' is equal to 'n'.
• If true, enter a nested for loop with 'k' initialized to 'i' and the condition 'k'
less
• than 'j'.
• Within the nested for loop, check if 'k' is equal to 'i'.
• If true, print 'k'.
• Otherwise, print " + " followed by 'k'.
• Print a newline character to move to the next line.
• Exit the nested for loop.
• Exit the if condition.
• End the for loop.
• STOP.

14
JAVA PROGRAM
import java.util.Scanner;
class number
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter a number "); //inputing the number
int n=sc.nextInt();
int sum=0,j=0;
for(int i=1;i<n;i++)
{
sum=i;
j=i+1;// adding consecutive natural numbers till sum is less than the given number
while(sum<n)
{
sum=sum+j;
j++;
}

if(sum==n)
{
for(int k=i;k<j;k++)
{
if(k==i)
System.out.print(k);
else
System.out.print(" + "+k);
}
System.out.println(); }
} } }

15
OUTPUT
Enter a number
25
3+4+5+6+7
12 + 13
Enter a number
105
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14
6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15
12 + 13 + 14 + 15 + 16 + 17 + 18
15 + 16 + 17 + 18 + 19 + 20
19 + 20 + 21 + 22 + 23
34 + 35 + 36
52 + 53

16
Question 4: - Write a Java program to find and display the saddle
point(s) in a given 2D array. A saddle point in a matrix is an
element which is the smallest element in its row and the largest
element in its column.
EXAMPLE: -
Enter the number of rows
3
Enter the number of columns
2
Enter elements of array:
12
13
15
24
35
69
Saddle
row:0 column:1

17
ALGORITHM
• START
• Initialize a Scanner object for input
• Prompt the user to enter the number of rows (m)
• Read and store the input in the variable m
• Prompt the user to enter the number of columns (n)
• Read and store the input in the variable n
• Declare a 2D array a of size m x n
• Print "Enter elements of array:" to the console
• Use nested loops to input elements into the array:
o Outer loop (i): Iterate from 0 to m - 1
o Inner loop (j): Iterate from 0 to n - 1
o Read and store the input in the array element a[i][j]
• Initialize variables h to store the highest element in the row, p to store the
column position of the highest element, c to store the lowest element in the
particular column, and b to track saddle points found (initially set to 0)
• Iterate through each row of the array:
o Inner loop (j): Iterate from 0 to n - 1
o Compare the current element a[i][j] with h (highest element in the row) and
update h and p accordingly
o Call the saddle function with h, p, m, n, and a as arguments to get the lowest
element in the particular column
o Check if the lowest element in the column (c) is equal to h (highest element in
the row)

18
o If equal, print "Saddle" and the row and column position of the saddle point.
Increment b and break the loop
o Reset p to -1
• If no saddle point is found (b is still 0), print "No Saddle Point"
• Define a function saddle that takes h, p, m, n, and the array a as arguments
• Initialize a variable l to store the lowest element in the particular column (
(initially set to the first element of the column)
• Iterate through the rows of the array:
o Check if the current element in the column (a[i][p]) is less than l, update l
accordingly
• Return l as the lowest element in the column
• STOP

19
JAVA PROGRAM
import java.util.*;
public class saddle_point //class begins
{
public static void main(String[] args) // main function
{
Scanner ab=new Scanner(System.in);
System.out.println("Enter the number of rows ");
int m=ab.nextInt(); //row numbers
System.out.println("Enter the number of columns ");
int n=ab.nextInt(); //column number
System.out.println("Enter elements of array:");
int a[][]=new int[m][n]; //array declaration
int h=0, p=-1, c=0,b=0;
for (int i=0;i<m;i++) //array input from user
{
for(int j=0;j<n;j++)
{
a[i][j]=ab.nextInt();
}
}
for (int i=0;i<m;i++) //checks for saddle point
{
for(int j=0;j<n;j++) // gives highest element from row
{
if(h<a[i][j])
{
h=a[i][j];
p=j; } }
c=saddle(h,p,m,n,a);
if(c==h) //condition for saddle
{
System.out.println("Saddle");
System.out.println("row:"+i+" column:"+p);
b++;
break; }
p=-1; }
if(b==0)
System.out.println("No Saddle Point");
}
static int saddle(int h, int p, int m, int n, int a[][]) //returns lowest element from particular column
{
int l=a[0][p];
for(int i=0;i<m;i++)
{

20
if(a[i][p]<l)
l=a[i][p];
}
return l; } }

OUTPUT
Input: Mat[3][3] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output: 7
7 is minimum in its row and maximum in its column.
Input: Mat[3][3] = {{1, 2, 3},
{4, 5, 6},
{10, 18, 4}}
Output: No saddle point

21
Question 5: - Given a number n, write a program to print all
prime factors of n.

For example

If the input number is 12,


Then the output should be “2 2 3”.
And if the input number is 315,
Then the output should be “3 3 5 7”.

Algorithm

• START
• Input a no. in variable n
• Initialize variable v and c
• While n is divisible by 2, print 2 and divide n by 2.
• After step 4, n must be odd.
• Now start a loop from i = 3 to the square root of n.
• While i divides n, print i, and divide n by i.
• After i fails to divide n, increment i by 2 and continue.
• If n is a prime number and is greater than 2,
• Then n will not become 1 by the above two steps.
• So print n if it is greater than 2.
• STOP
22
JAVA PROGRAM
import java.util.*;
class primefactorization
{
public static void main()
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt(); int c=0; int t=0;
for (int i = 2; i<=n;i++)
{
while(n%i==0)
{
t=i;
c++;
n=n/i;

}
if (t!=0)
{
System.out.println(t+" = "+ c );
}
t=0;
c=0;
}
} }

23
Question 6: - Given a 2D array, print all the no. till its square in
spiral form.

Examples:

Input: n = 4
Output: -
{{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13, 14, 15,16 }}

24
ALGORITHM
• START
• Inputting the size of square matrix form the main function
• initialize variable k to 1 and m to n-1 and r to 0
• Initialize a do while loop to perform the following functions
• -set a for loop for with i = r to i less than m and i increases by 1 filling no in
the first column. K increases by 1 every time for loop executes
• -set a for loop for with i = r+1 to i less than m and i increases by 1 filling no
in the next column. K increases by 1 every time for loop executes
• -set a for loop for with i = m-1 to i greater than equal to r and i decreases
by 1 filling no in the next column. K increases by 1 every time for loop
executes
• -set a for loop for with i = m-1 to I greater than m and i decreases by 1
filling no in the next column. K increases by 1 every time for loop executes
• Increase r by 1 and decrease m by 1.
• Repeat these steps while k is less than equal to square to the size of the
square matrix.
• Now Initialize a nested for loop with variable j and I to print the final
matrix.
• STOP

25
JAVA PROGRAM
class Spiral
{
public static void main(int n)
{
int a[][]=new int[n][n];
int k=1 , m=n-1, r=0;
do
{
for(int i=r;i<=m;i++)
{
a[r][i]=k++;
}
for(int i=r+1;i<=m;i++)
{
a[i][m]=k++;
}for(int i=m-1;i>=r;i--)
{
a[m][i]=k++;
}for(int i=m-1;i>r;i--)
{
a[i][r]=k++;
}
r++;
m--;
}while(k<=n*n);
for (int j=0;j<n;j++)
{
for (int i=0;i<n;i++)
{
System.out.print(a[j][i]+ " ");
}
System.out.println(); } } }

Output
Input: n = 5
Output {{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15}
{16,17,18,19,20}
{21,22,23,24,25}

26
Question7:- Write a program to create an array of size nxn and
fill ‘@’ as boundary elements , ‘*’ as diagonal elements and ‘#’ as
rest of the element and print the matrix.

ALGORITHM

• START
• Prompt the user to enter the size of the matrix.
• Read the size of the matrix from the user.
• Create a 2D character array a of size n x n, where n is the size of the matrix.
• Iterate over each row i from 0 to n-1.
o Inside the outer loop, iterate over each column j from 0 to n-1.
o Check if the current cell (i, j) is on the border of the matrix.
o If i is 0 or j is 0 or i is n-1 or j is n-1, set a[i][j] to '@'.
o Check if the current cell (i, j) lies on either of the diagonals.
o If i is equal to j or i+j is equal to n-1, set a[i][j] to '*'.
o If none of the above conditions are true, set a[i][j] to '#'.
• Print the matrix a row by row.
• STOP

27
JAVA PROGRAM
import java.util.*;

class matrix

public static void main()

Scanner sc = new Scanner (System.in);

System.out.println("ENTER SIZE OF MATRIX");

int n = sc.nextInt();

char a[][];

a= new char [n][n];

for (int i = 0 ; i<n;i++)

for(int j=0;j<n;j++)

if(i==0||j==0||i==n-1||j==n-1)

a[i][j]='@';

else if (i==j||i+j==n-1)

a[i][j]='*';

else

a[i][j]='#';

for (int i = 0 ; i<n;i++)

for(int j=0;j<n;j++)

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

System.out.println(); } } }

28
Question 8: - Write a program to input a 2d matrix with size
MxN. Arrange the first diagonal in ascending order and the
second diagonal in descending order.
Example: -
no. of rows
3
no. of coloumns
3
Input the values of the matrix
1
2
3
4
5
6
7
8
9
Original Matrix
123
456
789
Final Matrix

29
127
456
389

ALGORITHM
• START
• Create a scanner object “obj” to input from the users.
• Prompt the user to input the no. of rows and columns and store them in the
variable m and n.
• Prompt the user to input the values of the matrix
• Print the original matrix.
• Set a for loop with variable i equal to 0 till i less than m and keep on
increasing i by 1.
• Inside the i for loop initialize another for loop with variable j equal to 0 till j
less than n and keep on increasing j by 1.
• Now take the access of the first diagonal using the above nested for loop
compare and switch the values of the first diagonal to arrange them in
ascending order using a temporary variable temp.
• Set a for loop with variable i equal to 0 till i less than m and keep on
increasing i by 1.
• Inside the i for loop initialize another for loop with variable j equal to i+1 till j
less than n and keep on increasing j by 1.
• Now take the access of the second diagonal using the above nested for loop
compare and switch the values of the second diagonal to arrange them in
descending order using a temporary variable temp.
• Print the new arranged matrix.
• STOP

30
JAVA PROGRAM
import java.util.*;

public class ArrangeDiagonals


{
public static void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the no. of rows");
int m = obj.nextInt();
System.out.println("Input the no. of coloumns");
int n = obj.nextInt();
System.out.println("Input the values of the matrix");
int matrix [][] = new int [m][n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
matrix[i][j]= obj.nextInt();
}
}
System.out.println("Original Matrix");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
// Sort the first diagonal in ascending order.
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < n; j++) {
if (matrix[i][i] > matrix[j][j]) {
int temp = matrix[i][i];
matrix[i][i] = matrix[j][j];
matrix[j][j] = temp;
}
}
}

// Sort the second diagonal in descending order.


for (int i = 0; i < m; i++) {
for (int j = i + 1; j < n; j++) {
if (matrix[i][n - 1 - i] < matrix[j][m - 1 - j]) {
int temp = matrix[i][n - 1 - i];
matrix[i][n - 1 - i] = matrix[j][m - 1 - j];

31
matrix[j][m - 1 - j] = temp;
}
}
}

// Print the sorted matrix.


System.out.println("Final Matrix");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
} } }

OUTPUT
Input the no. of rows
4
Input the no. of coloumns
4
Original Matrix
1234
5678
9 10 11 12
13 14 15 16

Final Matrix
1 2 3 13
5 6 10 8
9 7 11 12
4 14 15 16

32
Question 9: - Write an program to generates combinations of
numbers without repetition for different lengths of digits.
Example: -
Input value = 3
Output
123
132
213
231
312
321

33
ALGORITHM
1. Start

2. Define a class cmbnatn.

3. Define methods inside cmbnatn for generating combinations of numbers without


repetition for different lengths of digits:
a. Method disp for generating combinations of 2-digit numbers without
repetition.
b. Method disp1 for generating combinations of 3-digit numbers without
repetition.
c. Method disp2 for generating combinations of 4-digit numbers without
repetition.
d. Method disp3 for generating combinations of 5-digit numbers without
repetition.
e. Method disp4 for generating combinations of 6-digit numbers without
repetition.
f. Method disp5 for generating combinations of 7-digit numbers without
repetition.

4. Implement each method to generate combinations using nested loops.


a. Start a loop from 1 to n-1 where n is the length of the desired combination.
b. Inside the outer loop, start another loop from 1 to n-1.
c. Check if the two loop variables are equal. If yes, continue to the next iteration.
d. Otherwise, print the combination of the loop variables.

34
5. In the main method:
a. Read an integer n from the user to determine the length of combinations to
generate.
b. Create an object of the cmbnatn class.
c. Use a switch-case statement to call the appropriate method based on the
value of n:
- If n is 2, call disp method.
- If n is 3, call disp1 method.
- If n is 4, call disp2 method.
- If n is 5, call disp3 method.
- If n is 6, call disp4 method.
- If n is 7, call disp5 method.

6. End

35
JAVA PROGRAM
import java.util.Scanner;

class cmbnatn
{
void disp()
{
for(int i = 1;i<=2;i++)
{
for(int j = 1 ;j<=2;j++)
{
if(i==j)
{
continue;
}
else
{
System.out.println(i+""+j);
}
}
}
}

void disp1()
{
for(int i = 1; i<=3;i++)
{
for(int j = 1 ; j<=3;j++)
{
if(i==j)
{
continue;
}
else
{
for(int k = 1;k<=3;k++)
{
if(k==i || k==j)
{
continue;
}
else
{
System.out.println(i+ "" +j+ "" +k);
} } } } } }

36
public static void main()
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
cmbnatn obj = new cmbnatn();
if(n==2)
obj.disp();
if(n==3)
obj.disp1();
if(n==4)
obj.disp2();
if(n==5)
obj.disp3();
if(n==6)
obj.disp4();
if(n==7)
obj.disp5();
}

void disp2()
{
for(int i = 1;i<=4;i++)
{
for(int j = 1;j<=4;j++)
{
if(i==j)
{
continue;
}
else
{
for(int k = 1;k<=4;k++)
{
if(k==i||k==j)
{
continue;
}
else
{
for(int l = 1;l<=4;l++)
{
if(l==i||l==j||l==k)
{
continue;
}

37
else
{
System.out.println(i+""+j+""+k+""+l);
}
} } } } } } } }

void disp3()
{
for(int i = 1;i<=5;i++)
{
for(int j = 1;j<=5;j++)
{
if(i==j)
{
continue;
}
else
{
for(int k = 1;k<=5;k++)
{
if(k==i||k==j)
{
continue;
}
else
{
for(int l = 1;l<=5;l++)
{
if(l==i||l==j||l==k)
{
continue;
}
else
{
for(int m =1 ; m<=5;m++)
{
if(m==i||m==j||m==k||m==l)
{
continue;
}
else
{
System.out.println(i+""+j+""+k+""+l+""+m);
}
} } } } } } } } }

38
void disp4()
{
for(int i = 1;i<=6;i++)
{
for(int j = 1;j<=6;j++)
{
if(i==j)
{
continue;
}
else
{
for(int k = 1;k<=6;k++)
{
if(k==i||k==j)
{
continue;
}
else
{
for(int l = 1;l<=6;l++)
{
if(l==i||l==j||l==k)
{
continue;
}
else
{
for(int m =1 ; m<=6;m++)
{
if(m==i||m==j||m==k||m==l)
{
continue;
}
else
{
for(int n = 1; n<=6;n++)
{
if(n==i||n==j||n==k||n==l||n==m)
{
continue;
}
else
{
System.out.println(i+""+j+""+k+""+l+""+m+""+n);
}

39
}
}
} } } } } } } } }
void disp5()
{
for(int i = 1;i<=6;i++)
{
for(int j = 1;j<=6;j++)
{
if(i==j)
{
continue;
}
else
{
for(int k = 1;k<=6;k++)
{
if(k==i||k==j)
{
continue;
}
else
{
for(int l = 1;l<=6;l++)
{
if(l==i||l==j||l==k)
{
continue;
}
else
{
for(int m =1 ; m<=6;m++)
{
if(m==i||m==j||m==k||m==l)
{
continue;
}
else
{
for(int n = 1; n<=6;n++)
{
if(n==i||n==j||n==k||n==l||n==m)
{
continue;
}
else

40
{
for(int o =1;o<=7;o++)
{
if(o==i || o==j || o==k || o==l || o==m || o==n)
{
continue;
}
else
{
System.out.println(i+""+j+""+k+""+l+""+m+""+n+""+o);
}
}
} } } } } } } } } } }
}

41
Question 10: - Write a program to input a 2d matrix of size m by n first Arrange
row in ascending order. Then arrange column in ascending order.
Example: -
ENTER THE VALUE OF M: 3
ENTER THE VALUE OF N: 3
ENTER ELEMENTS OF MATRIX:
ENTER ELEMENTS OF ROW 1: 1,2,3
ENTER ELEMENTS OF ROW 2: 4,5,6
ENTER ELEMENTS OF ROW 3: 7,8,9
ORIGINAL MATRIX
123
456
789
MATRIX AFTER SORTING ROWS
123
456
789
MATRIX AFTER SORTING BOTH ROWS IN ASCENDING AND COLOUMNS IN
DESCENDING
789
456
123

42
ALGORITHM
1. Start
2. Prompt the user to enter the values of M and N
3. Read the values of M and N
4. Create a 2D array 'a' of size MxN
5. Prompt the user to enter the elements of the matrix
6. Read the elements of the matrix into the array 'a'
7. Display "Original Matrix"
8. Loop through each row 'i' from 0 to M-1
a. Loop through each column 'j' from 0 to N-1
i. Print the value of a[i][j]
b. Print a new line
9. Sort each row of the matrix 'a' in ascending order
10. Display "Matrix After Sorting Rows"
11. Loop through each row 'i' from 0 to M-1
a. Loop through each column 'j' from 0 to N-1
i. Print the value of a[i][j]
b. Print a new line
12. Sort each column of the matrix 'a' in descending order
13. Display "Matrix After Sorting Rows in Ascending and Columns in Descending"
14. Loop through each row 'i' from 0 to M-1
a. Loop through each column 'j' from 0 to N-1
i. Print the value of a[i][j]

43
b. Print a new line
15. End

JAVA PROGRAM
import java.util.Scanner;

public class ArraySort


{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF M: ");
int m = in.nextInt();
System.out.print("ENTER THE VALUE OF N: ");
int n = in.nextInt();
int a[][] = new int[m][n];
System.out.println("ENTER ELEMENTS OF MATRIX:");
for (int i = 0; i < m; i++) {
System.out.println("ENTER ELEMENTS OF ROW " + (i+1) + ":");
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
}
}

System.out.println("ORIGINAL MATRIX");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}

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


for (int j = 0; j < n - 1; j++) {
for (int k = 0; k < n - j - 1; k++) {
if (a[i][k] > a[i][k + 1]) {
int t = a[i][k];
a[i][k] = a[i][k+1];
a[i][k+1] = t;
}
}
}
}

44
System.out.println("MATRIX AFTER SORTING ROWS");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}

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


for (int j = 0; j < m - 1; j++) {
for (int k = 0; k < m - j - 1; k++) {
if (a[k][i] < a[k + 1][i]) {
int temp = a[k][i];
a[k][i] = a[k + 1][i];
a[k + 1][i] = temp;
}
}
}
}

System.out.println("MATRIX AFTER SORTING BOTH ROWS IN ASCENDING AND COLOUMNS IN


DESCENDING");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}

45

You might also like