Assignment 16:
Bouncy Number
A bouncy number is any non-negative integer that is neither increasing nor decreasing.
An increasing integer is any integer in which when going from left to right, the current
digit is greater than or equal to the previous digit.
For example, 1234 and 14779 are both increasing integers.
In contrast, a decreasing integer is any integer in which when going from left to right,
the current digit is less than or equal to the previous digit.
Some examples of decreasing integers are 6432 and 988743.
A bouncy number’s digits neither increase nor decrease. Rather, they “bounce” between
increasing and decreasing.
Example: 12435 or 1441.
Write a program to accept a positive number and check whether it is a Bouncy number
or not.
Algorithm:
Step 1: Start
Step 2: Accept a positive number n.
Step 3: Declare and initialize f=0,f1=0
Step 4: Check the digits are in increasing order or not. If false then go to step 5 else go to
step 6
Step 5: Change the value of f=1.
Step 6: Check if the digits are in decreasing order or not. If false then go to step 7 else go
to 8
Step 7: Change the value of f1=1.
Step 8: Check f=1 and f1=1. If true then go to step 9 else go to step 10
Step 9: Print n, “is a Bouncy Number”
Step 10: Print n, “is not a Bouncy Number”
Step 11:Stop
Program:
/* Program to check whether the given number is a Bouncy number or not.*/
import java.util.*;
class BouncyNumber
public void isBouncy()
Scanner sc= new Scanner(System.in); System.out.println("Enter a number:");
int n=sc.nextInt(); int c=0, a=n;
//counting number of digits while(a>0)
a=a/10; c++;
int f=0,inc=0,d;
//checking whether it is in increasing order
a=n;
for(int j=c;j>=1;j--)
{
d=a/(int)Math.pow(10,j-1);//Extracting the left most digit
if(d>=inc)
inc=d; }
else
f=1; break;
a=a%(int)Math.pow(10,j-1);
//checking whether the digits are in decreasing order
int dec=9;
a=n;
for(int j=c;j>=1;j--)
d=a/(int)Math.pow(10,j-1);//Extracting the left most digit
if(d<=dec)
dec=d;
} else {
f1=1; break;
}
a=a%(int)Math.pow(10,j-1);
if(f==1&&f1==1)
System.out.println(n+" is a Bouncy Number.");
else
System.out.println(n+" is not a Bouncy Number.");
}
Assignment 17:
Calculating Denomination and number of notes
Design a program to accept the amount from the user and display the break – up in
descending order of denomination along with the total number of notes. The available
denominations are 2000, 500, 200, 100, 50, 20, 10. Also print the amount in words
according to the digit.
Example:
Enter Amount:
3450
THREE FOUR FIVE ZERO
Denominations:
2000 x 1 = 2000
500 x 2 = 1000
200 x 2 = 400
50 x 1 = 50
Total Number of Notes : 6
Algorithm:
Step 1: Start
Step 2: Accept an amount amt
Step 3: Check amt is in the given denomination. If true then go to step 4 else go to step
11
Step 4: Create an integer array and store the denominations.
Step 5: Create a string array and store the digits in words.
Step 6: Extract each digit from the left and concatenate the digits in words in a string w.
Step 7: Print the concatenated word w.
Step 8: Divide the amount with each denomination and add the quotient into the
variable total.
Step 9: Print the denominations.
Step 10: Print total number of notes, total
Step 11: End
Program:
import java.util.*;
class Money
public void deno()
Scanner sc= new Scanner(System.in);
System.out.println("Enter Amount:");
int amt= sc.nextInt();
if(amt%10!=0)
System.out.println("Invalid Amount.");
else
int d[]={2000,500,200,100,50,20,10};
String
wrd[]={"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT",
"NINE"};
String w="";
int total=0,a=amt,r=0;
int l=Integer.toString(amt).length(); //calculating number of digits
for(int i=l;i>0;i--)
r=a/(int)Math.pow(10,i-1);
w=w+wrd[r]+" ";
a=a%(int)Math.pow(10,i-1);
System.out.println(w);
System.out.println("Denomination:");
a=amt;
for(int i=0;i<d.length;i++)
r=a/d[i];
if(r>0)
total=total+r;
System.out.println(d[i]+ "x" + r+"="+d[i]*r);
a=a%d[i];
System.out.println("Total number of notes:"+total);
}
}
OUTPUT:
Assignment 18:
Arranging words containing vowel in beginning
Write a program to accept a paragraph of two sentence. The sentences can be
terminated with either by ‘ ?’ or ‘!’ or ‘ .’ . The words are to be separated by single blank
space and must in Upper Case.
Perform the following tasks.
(a) Check the validity for the terminated characters.
(b) Create a new string with the words that begins and end with vowels followed by
other words.
(c) Display the new sentence along with the original sentence.
Example 1:
Input: An Apple a day keeps the doctor away.
ORIGNAL SENTENCE:
AN APPLE A DAY KEEPS THE DOCTOR AWAY.
REARRANGED SENTENCE:
APPLE A AN DAY KEEPS THE DOCTOR AWAY
Example 2:
Input 2: How are you
Invalid String.
Algorithm:
Step 1: Start
Step 2: Accept a sentence st.
Step 3: Convert it into uppercase.
Step 4: Calculate its length l.
Step 5: Check whether the string ends with ‘?’ or ‘!’ or ‘.’. If not then go to step 6 else
go to step 7
Step 6: Print “Invalid String”. Go to step 18
Step 7: Declare and initialize variables i=0, vow=”” and cons=””
Step 8: Repeat the steps 9 to 14 until i=l
Step 9: Extract a word wrd.
Step 10: Check whether the first and last character of the wrd is a vowel or
not. If yes then go to step 11 else go to step 12
Step 11: Concatenate vow, wrd and a space and go to step 13.
Step 12: Concatenate cons, wrd and a space.
Step 13: Update wrd=””,
Step 14: Update i=i+1
Step 15: Create a new string by concatenating vow and cons.
Step 16: Print st
Step 17: Print nst
Step 18: Stop
Program:
import java.util.*;
class Vowels
{
public void merge()
Scanner sc= new Scanner(System.in);
System.out.println("Enter a sentence:");
String st= sc.nextLine();
st=st.toUpperCase(); //coverting to Upper Case
int l=st.length();
char last=st.charAt(l-1); // extracting last character
//checking the validity of the last character
if(last!='.' && last!='?'&&last!='!' )
System.out.println("Invalid String.");
else
String nst="", wrd="",vow="",cons="";
char ch=' ', ch1=' ', ch2= ' ';
int wl=0;
for(int i=0;i<l;i++)
ch=st.charAt(i);
//Extracting each word
if(ch!=' '&&ch!='?'&&ch!='!'&&ch!='.')
wrd=wrd+ch;
else
wl=wrd.length();
ch1=wrd.charAt(0); //Extracting first character of the word
ch2=wrd.charAt(wl-1); //Extracting last character of the word
//Checking whether both characters are vowel or not
if((ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U')
&&(ch2=='A'||ch2=='E'||ch2=='I'||ch2=='O'||ch2=='U'))
vow=vow+wrd+" "; // joins the word begin and end with vowels
else
cons=cons+wrd+" "; // joins the other words
wrd="";
nst=vow+cons;
System.out.println(st);
System.out.println(nst);
}
Variable Description
Variable Data type Description
st String The sentence
l Int Length of the sentence
Terminating character of the
last Char
string
To extract each character of the
ch Char
string
wrd String To extract each word of the string
Loop control variable to generate
i Int the index number of each
character
wl Int Length of the word
ch1 Char First character of the word
ch2 Char Last character of the word
Words that begin and end with
vow String
vowel
cons String Other words
nst String New word
Output:
Assignment 19:
Displaying Date
Design a program to accept a day number (between 1 and 366), year(in 4 digits) from
the user to generate and display the corresponding date. Display an error message if the
value of the day number and year are not within the limit or not according to the
condition specified.
Test your program with the following data and some random data:
INPUT:
EXAMPLE 1: DAY NUMBER: 255
YEAR: 2018
OUTPUT: DATE: 12 SEPTEMBER, 2018
Algorithm:
Step 1: Start
Step 2: Accept number of days d.
Step 3: Accept year y.
Step 4: Check d>=1 and d<=366 and y>=1000 and y<= 10000. If true go to step
5 else go to step 14
Step 5: Create and initialize an array, mdays[] with number of days in each
month.
Step 6: Create and initialize an array, months [] with the month names.
Step 7: Check y is a Leap year or not. If true go to step 8.
Step 8: Increase the value of mdays[2] by 1. Go to step 9
Step 9: Declare and initialize i=1
Step 10: Repeat the steps11 to12 until d<= mdays[i]
Step 11: Reduce d=d-mdays[i]
Step 12: Update i =i+1
Step 13: Print d, mdays[i], y. Go to step 15
Step 14: Print “Invalid Date”
Step 15: Stop
Program:
import java.util.*;
public class Day
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("DAY NUMBER:");
int d=sc.nextInt();
System.out.println("ENTER YEAR");
int y=sc.nextInt();
if((d>=1&&d<=366)&&(y>=1000&&y<=9999))
int mdays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
String months[]= {"","JANUARY","FEBRUARY","MARCH",
"APRIL","MAY","JUNE","JULY",
"AUGUST","SEPTEMBER",
"OCTOBER","NOVEMBER","DECEMBER"};
if(y%4==0)//checking for leap year
mdays[2]=29;
int i=1;
//checking whether the number of days greater than each month
while(d>mdays[i])
d=d-mdays[i];//reducing no of days of the month
i++;
System.out.println("Date:"+ d+" "+months[i]+","+ y);
else
System.out.println("Invalid date");
}
Variable Description
Variable Data type Description
d int Number of days
year Int Year
mdays[] Int Array to store total number of
days of each month.
months[] String Array to store the name of each
month.
i Int Loop control variable to count the
month.
Output:
Assignment 20:
Rearranging Matrix
Write a program to declare a single-dimensional array a[] and a square matrix b[][] of
size N, where N > 2 and N < 10. Allow the user to input positive integers into the single
dimensional array.
Perform the following tasks on the matrix:
1. Sort the elements of the single-dimensional array in ascending order using any
standard sorting technique and display the sorted elements.
2. Fill the square matrix b[][] in the given format:
If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8}
Then, the matrix b[][] would fill as below:
1 2 5 8
1 2 5 1
1 21 2
11 2 5
Algorithm:
Step 1: Accept order of the Matrix N.
Step 2: Check N<3 or N>9. If true then go to step 3 else go to 4
Step 3: Print “Out of Range”. Go to step 21
Step 4: Create a Single Dimensional array B [] of order N
Step 5: Create a double dimensional array A[][] of order N x N.
Step 6: Store elements in the array B [].
Step 7: Sort the array using Bubble sort.
Step 8: Display the sorted array.
Step 9: Declare and initialize the variables i=0, n=N-1
Step 10: Repeat the steps 11 to 19 until i=n.
Step 11: Declare and initialize a variable j=0
Step 12: Repeat the step 13 to 14 until j>n
Step 13: Assign A[i][j] = B[j]
Step 14: Increase j by 1
Step 15: Declare and initialize the variables k=n+1, p=0
Step 16: Repeat the steps 17 to 18 until k=N
Step 17: Assign A[i][k] =B[p]
Step 18: Increase the variables k and p by 1
Step 19: Increase by 1 and decrease N by 1
Step 20: Print the Matrix
Step 21:Stop
Program:
import java.util.*;
public class MatrixPattern
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter the order of the Matrix:");
int N=sc.nextInt();
if(N<3 || N>9)
System.out.println("Out of range");
else
int B[]= new int[N];
int A[][]= new int[N][N];
for(int i=0;i<N;i++)
System.out.println("Enter a number:");
B[i]= sc.nextInt();
//sorting
int t=0;
for(int i=0;i<N;i++)
for(int j=0;j<N-1-i;j++)
if(B[j+1]<B[j])
t=B[j];
B[j]=B[j+1];
B[j+1]= t;
}
//printing the sorted array
System.out.println("Sorted Array is:");
for(int i=0;i<N;i++)
System.out.print(B[i]+" ");
for(int i=0,n=N-1;i<N;i++,n--)
for(int j=0;j<=n;j++)//storing upper pattern
A[i][j]=B[j];
for(int k=n+1, p=0;k<N;k++,p++)//storing lower pattern
A[i][k]=B[p];
//Displaying the Matrix
System.out.println("\n The Matrix is:");
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
System.out.println();
Variable Description:
Variable Data Type Description
B[] Int To store the array
A[][] Int To store the matrix
M Int Number of rows
N Int Number of columns
i, j Int Looping Variables
T Int Sorting Variable
k, p Int Variables for restoring elements in thr array
Output:
Assignment 21:
Mirror Image
Write a program in Java to enter natural numbers in a double dimensional array m x n
(where m is the number of rows and n is the number of columns). Display the new
matrix in such a way that the new matrix is the mirror image of the original matrix.
Sample Input:
8 15 9 18
9 10 7 6
10 8 11 13
12 16 17 19
Sample Output:
18 9 15 8
6 7 10 9
13 11 8 10
19 17 16 12
Algorithm:
Step 1: Start
Step 2: Accept number of rows (M) and number of columns (N).
Step 3: Check whether M>2, M<=10, N>2 and N<=10. If true then go to step 4 else go to
step 11.
Step 4: Create a Matrix A[][] of order MxN and store elements.
Step 5: Print the matrix A[][].
Step 6: Repeat the steps 7 to 9 for c=0 to N-1 increase by 1.
Step 7: Repeat the steps 8 to 9 for i=0 to M-1 increase by 1.
Step 8: Repeat the step 9 for j=N-1 to c+1 decrease by 1.
Step 9: Swap the elements of A[i][j] and A[i][j-1].
Step 10: Print the Mirror Image of the Matrix A[][].
Step 11: Stop
Program:
import java.util.*;
class Mirror
public void coumnSort()
Scanner sc= new Scanner(System.in);
System.out.println("Enter number of rows:");
int M=sc.nextInt();
System.out.println("Enter number of columns:");
int N=sc.nextInt();
if(M<=2||N<=2||M>10||N>10)
System.out.println("OUT OF RANGE");
else
int A[][]= new int[M][N];
//Storing elements in the array
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.println("Enter a number:");
A[i][j]=sc.nextInt();
//Printing the Original Matrix
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]+"\t");
System.out.println();
//Creating Mirror Image in the same array by shifting columns
int t=0;
for(int c=0;c<N;c++)
{
for(int i=0;i<M;i++)
for(int j=N-1;j>c;j--)
t=A[i][j];
A[i][j]=A[i][j-1];
A[i][j-1]=t;
//Printing the Mirror Image of the Matrix
System.out.println("MIRROR IMAGE OF THE MATRIX:");
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
Variable Description:
Variable Data Type Description
A[][] Int To store the matrix
M Int Number of rows
N Int Number of columns
i, j Int Looping Variable
t, c Int Variables to create mirror image
Output:
Assignment 22:
Smith Number
A Smith number is a composite number, whose sum of the digits is equal to the sum of
its prime factors. For example:
4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers.
Write a program in Java to enter a number and check whether it is a Smith number or
not.
Sample Input: 666
Sum of the digits: 6 + 6 + 6 = 18
Prime factors are: 2, 3, 3, 37
Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18
Thus, 666 is a Smith Number.
Algorithm:
Step 1: Start
Step 2: Accept a number n.
Step 3: Check whether it not a composite number . If true then go to step 4 else go to
step 5.
Step 4: Print n, “is not a Composite Number” and go to step
Step 5: Calculate the sum of digit of digits of n(dsum).
Step 6: Calculate the sum of digits of its prime factor(sp);
Step 7: Check whether dsum=sp. If true then go to step 8 else go to step 9.
Step 8: Print n, “is a Smith Number.” and go to step 10.
Step 9: Print n, is not a Smith Number”.
Step 10: Stop
Program:
import java.util.*;
public class Smith
int n;
public void accept()
Scanner sc = new Scanner(System.in);
System.out.println("Enter number:");
n = sc.nextInt();
int sumDigits(int x)
int s=0;
while(x>0)
s=s+x%10;
x=x/10;
return s;
boolean isPrime(int a)
{
int c=0;
for(int i=1;i<=a;i++)
if(a%i==0)
c++;
if (c==2)
return true;
else
return false;
void check()
//checking whether n is composite or not
int f=0;
for(int i=2;i<n;i++)
if(n%i==0)
f=1;
break;
}
}
if(f==0)
System.out.println(n+"is not a Composite Number.");
else
int dsum=sumDigits(n);
int sp=0,j=2;
int a=n;
while(a>1)
if(a%j==0&&isPrime(j)==true)
System.out.print(j+" ");
sp=sp+sumDigits(j);
a=a/j;
continue;
j++;
System.out.println("\nSum of digits:"+sp);
System.out.println("Sum of digits of Prime Factors:"+dsum);
if(dsum==sp)
System.out.println(n+" is a Smith Number.");
else
System.out.println(n+"is not a Smith Number.");
public static void main(String args[])
Smith obj = new Smith();
obj.accept();
obj.check();
Variable Description:
Assignment 23:
Consecutive Letters
Write a program to accept a string and display the words along with the frequency of the
words which have at least a pair of consecutive letters.
Sample Input:
Modem is an electronic device
Sample Output:
Algorithm:
Step 1: Start
Step 2: Accept a String st.
Step 3: Convert it into uppercase.
Step 4: Calculate its length (l);
Step 5: Repeat the steps 6 to 10 for i=0 to l-1 increases by 1.
Step 6: Extract each word of the sentence(wrd).
Step 7: Check whether wrd contains consecutive pair or not. If true then go to step 8 else
go
to step 10.
Step 8: Print wrd.
Step 9: Increase wordcount by 1. And go to step 10
Step 10: Set wrd=””.
Step 11: Print “Number of words contain consecutive pairs:”, wordcount.
Step 12: Stop
Program:
import java.util.*;
class Cons
public void display()
Scanner sc= new Scanner(System.in);
System.out.println("Enter a word:");
String st=sc.nextLine();
st=st.toUpperCase();
st=st+" ";
System.out.println(st);
int l=st.length();
int wordcount=0;
char ch=' ', ch1=' ',ch2=' ';
String wrd="";
for(int i=0;i<l;i++)
ch=st.charAt(i);
if(ch!=' ')
{
wrd=wrd+ch;
else
for(int j=0;j<wrd.length()-1; j++)
ch1=wrd.charAt(j);
ch2=wrd.charAt(j+1);
if((int)ch2-(int)ch1==1)
System.out.println(wrd);
wordcount++;
break;
wrd="";
System.out.println("Number of words contain consecutive letters:"+wordcount);
}
Variable Description:
Variable Data type Description
st Int To store the sentence
wrd Int To extract and store each word
l Int Length of the sentence (st)
ch,ch1,ch2 Int To store extracted characters
wordcount Int Number of words containing consecutive letters
i,j Int Looping Variables
Output:
Assignment 24:
Largest and Smallest element in each row
Write a program to create a matrix A[][] of order (MxN) and store the numbers in it.
(Both M and N should be greater than 2 and less than 10).Find the largest and smallest
element in each row.
Sample Input:
1 2 3
4 5 6
7 8 9
Sample Output:
Row Maximum Minimum
1 3 1
2 6 4
3 9 7
Algorithm:
Step 1: Start
Step 2: Accept number of rows (M).
Step 3: Accept number of columns (N).
Step 4: Check whether M<3, M>9, N<3 or N>9. If true then go to step 5 else go to step
6.
Step 5: Print “OUT OF RANGE.” and go to step 17
Step 6: Declare a matrix A[][] of order and store elements in it.
Step 7: Print the matrix A[][].
Step 8: Repeat the steps 9 to 16 for i=0 to M-1 increase by 1.
Step 9: Set max=A[i][0]
Step 10: Set min=A[i][0]
Step 11: Repeat the steps 12 to 15 for j=1 to N-1 increase by 1
Step 12: Check whether A[i][j]>max. If true then go to step13 else go to step 14.
Step 13: Set max= A[i][j]
Step 14: Check whether A[i][j]<min. If true then go to step15 else go to step 15.
Step 15: Set min= A[i][j] and go to step 16.
Step 16: Print (i+1), max, min.
Step 17: Stop
Program:
import java.util.*;
class MinMax
public void find()
Scanner sc= new Scanner(System.in);
System.out.println("Enter number of rows:");
int M=sc. nextInt();
System.out.println("Enter number of columns:");
int N=sc. nextInt();
if(M<3||N<3||M>9||N>9)
System.out.println("OUT OF RANGE");
else
int A[][]= new int[M][N];
for(int i=0;i<M;i++)
System.out.println("Enter row elements:");
for(int j=0;j<N;j++)
A[i][j]=sc.nextInt();
System.out.println("Matrix is:");
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
int min, max;
System.out.println("Row\tMaximum\tMinimum");
for(int i=0;i<M;i++)
min=A[i][0];
max=A[i][0];
for(int j=1;j<N;j++)
if(A[i][j]>max)
max=A[i][j];
if(A[i][j]<max)
min=A[i][j];
System.out.println((i+1)+"\t"+max+"\t"+min);
}
Variable Description:
Variable Data Type Description
A[][] Int To store the matrix
M Int Number of rows
N Int Number of columns
i, j Int Looping Variable
Min Int Smallest element of the row
Max Int Largest element of the row
OUTPUT:
Assignment 25:
Sorting the boundary elements
Write a program to create a matrix A[][] of order (MxN) and store the numbers in it.
(Both M and N should be greater than 2 and less than 10). Sort the boundary elements
of the matrix. Display the sorted matrix.
Sample Input:
11 8 34
45 21 1
9 18 74
Sample Output:
1 8 9
74 21 11
45 34 18
Algorithm:
Step 1: Start
Step 2: Accept number of rows (M).
Step 3: Accept number of columns (N).
Step 4: Check whether M<3, M>9, N<3 or N>9. If true then go to step 5 else go to step
6.
Step 5: Print “OUT OF RANGE.” and go to step 14
Step 6: Declare a matrix A[][] of order and store elements in it.
Step 7: Print the matrix A[][].
Step 8: Calculate size=2(M+N-2)
Step 9: Declare an array B[] of size.
Step 10: Store the boundary elements in the array B[].
Step 11: Sort the array B[] using bubble sort.
Step 12: Store the sorted border elements in the matrix A[][].
Step 13: Display the Sorted matrix A[][];
Step 14: Stop
Program:
import java.util.*;
class SortOuter
public void sort()
Scanner sc= new Scanner(System.in);
System.out.println("Enter number of rows:");
int M=sc.nextInt();
System.out.println("Enter number of columns:");
int N=sc.nextInt();
if(M<=2||N<=2||M>10||N>10)
System.out.println("OUT OF RANGE");
else
{
int A[][]= new int[M][N];
//Storing elements in the array
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.println("Enter a number:");
A[i][j]=sc.nextInt();
//Printing the Original Matrix
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]+"\t");
System.out.println();
int size=2*(M+N-2);
int B[]=new int[size];
//storing outer elements
int k=0;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
if(i==0||j==0||i==M-1||j==N-1)
B[k++]=A[i][j];
//sorting
int t=0;
for(int i=0;i<size;i++)
for(int j=0;j<size-1-i;j++)
if(B[j+1]<B[j])
t=B[j];
B[j]=B[j+1];
B[j+1]=t;
}
k=0;
for(int j=0; j<N;j++)
A[0][j]=B[k++];
for(int i=1; i<M;i++)
A[i][N-1]=B[k++];
for(int j=N-2; j>=0;j--)
A[M-1][j]=B[k++];
for(int i=M-2; i>0;i--)
A[i][0]=B[k++];
//Printing the Sorted Matrix
System.out.println("SORTED MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
Variable Description:
Variable Data Type Description
A[][] Int To store the matrix
B[] Int Array to store the elements of the matrix
M Int Number of rows
N Int Number of columns
i, j Int Looping Variable
K Int Variable to store matrix elements in the array(B[])
T Int To sort the elements in ascending order
Output: