You are on page 1of 148

# Page 1 of 148

Question 1:
Write a program to input a 5x5 double dimensional array and print it in E form.

Algorithm :
Step 1: Start
Step 2: Input a number from the user.
Step 3: Pass the number to the function isFascinating.
Step 4: Multiply the number by 1, 2 and 3 and store them in three different variables.
Step 5: Concatenate the variables in a String variable.
Step 6: Loop Starts.
Step 7: The "i" loop takes the number at index number 'i' and stores in variable 'c'. The "j" loop starts
from index number 'i+1' and checks whether the variable c is repeated in the remaining string or not.
Step 8: If the variable 'c' becomes equal to any other character in the remaing string then the counter
variable 'count' increments by 1.
Step 9: If count >1 then the 'i loop' breaks and the compiler comes out of the i loop.
Step 10: Then the compiler checks whter if i==num.length() or i<num.length().
Step 11: If i==num.length() then it means that there was no repeated words and the number
becomes a "Fascinating Number".
Step 12: End

Programming Code:
import java.util.*;
class E-form
{
public static void main(String args[])
{

Page 2 of 148

## Scanner sc = new Scanner(System.in);

int a[][]=new int[5][5];
System.out.pritln(Enter Array Containing 25 elements);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println(Original Matrix);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );

## System.out.println(Array in E form is :);

for(i=0;i<5;i++)
{
For(j=0;j<5;j++)
{
If(i%2==0)
System.out.print(a[i][j]);
}
System.out.println();

Page 3 of 148

}
}
}

Question 2:
Write a program in java to input a double dimensional square matrix having odd numbers of rows and
column. Print the array in T form.

algorithm:
step 1: start.
Step 2: Input the number from the user in variable 'n'.
Step 3: Pass the variable 'n' to the function isPronic.
Step 4: Loop starts.
Step 5: The loop checks whether the formal parameter having the copy of value of 'n' is equal to
i*(i+1) or not.
Step 6: If q==i*(i+1) then the counter value 'flag' becomes 1 and the loop breaks.

Page 4 of 148

Step 7: Then the compiler checks whether the value of 'flag' is 1 or not.
Step 8: If 'flag'==1 the number is "Pronic Number" otherwise not.
Step 9: End.

Programming Code:
import java.util.*;
class T-form
{
public static void main(String arg[])
{
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
System.out.println("Enter size of an Array from 2 to 20 that are odd ");
m=ob.nextInt( );
if(m>1 && m<=20)

Page 5 of 148
{
a=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{

a[i][j]=ob.nextInt( );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
System.out.println(Array in T form is :);
for(i=0;i<n;i++)
{
System.out.print(a[0][n]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n;j++)
{

Page 6 of 148
if(j==(n-1)/2)
System.out.println(a[i][j]);
}
}

Programming Code:
import java.util.*;
class N-form
{
public static void main(String arg[])
{
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
System.out.println("Enter size of an Array from 2 to 20 ");
m=ob.nextInt( );
if(m>1 && m<=20)

Page 7 of 148
{
a=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{

a[i][j]=ob.nextInt( );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
System.out.println(Array in N form is :);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(j==0||j==4||j==i)
System.out.print(a[i][j]);
}
System.out.println();

Page 8 of 148
}
}
}

import java.util.*;
class M-form
{
public static void main(String arg[])
{
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
a=new int[5][5];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)

Page 9 of 148
{

a[i][j]=ob.nextInt( );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
System.out.println(Aray in M form is :);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(j==0||j==4||(j==1&&i==1)||(j==2&&i==2)||(j==3&&i==1))
System.out.print(a[i][j]);
}
System.out.println();
}
}
}

Page 10 of 148

import java.util.*;
class X-form
{
public static void main(String arg[])
{
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
a=new int[5][5];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{

a[i][j]=ob.nextInt( );
}

Page 11 of 148
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
System.out.println(Array in X form is :);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(i==j||i+j==4)
System.out.print(a[i][j]);
}
}
}
}

import java.util.*;
class Y-form
{
public static void main(String arg[])
{

Page 12 of 148
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
System.out.println("Enter size of an Array from 2 to 20 ");
m=ob.nextInt( );
if(m>1 && m<=20)
{
a=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{

a[i][j]=ob.nextInt( );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );

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

Page 13 of 148
for(j=0;j<5;j++)
{
if((i==0&&j==0)||(i==1&&j==1)||(i==2&&j==2)||(i=1&&j==3)||(i=0&&j==4)||
(i==3&&j==2)||((i==4&&j==2))
System.out.print(a[i][j]);
}
System.out.println();
}
}
}

import java.util.*;
class Z-form
{
public static void main(String arg[])
{
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
a=new int[5][5];
for(i=0;i<5;i++)

Page 14 of 148
{
for(j=0;j<5;j++)
{

a[i][j]=ob.nextInt( );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
System.out.println(Array in the Z form is :);
for(i=0;i<5;i++)
{
System.out.print(a[0][j]+" ");
}
for(i=1;i<4;i++)
{
for(j=0;j<5;j++)
{
if(i+j==4)
System.out.print(a[i][j]+" ");
else

Page 15 of 148
System.out.print(" ");
}
System.out.println();
}
for(i=0;i<5;i++)
{
System.out.print(a[4][i]);
}
}
}

import java.util.*;
class L-form
{
public static void main(String arg[])
{
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
System.out.println("Enter size of an Array from 2 to 20 ");
m=ob.nextInt( );
if(m>1 && m<=20)
{
a=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)

Page 16 of 148
{

a[i][j]=ob.nextInt( );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
System.out.println( Array in L form is );
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
if(j==0)
System.out.print(a[i][j]+" ");
else
System.out.print(" ");
}
System.out.println();
}
for(i=0;i<5;i++)
{

Page 17 of 148
System.out.print(a[4][i]);
}

Question 9:
Write a Program in Java to input a D.D.A of order m x n where m is the number of rows and n is the
number of columns. Find the greatest number in the matrix and assign that number in the left
diagonal of the matrix. Print the new Matrix.

Algorithm :
Programming Code:
import java.util.*;
class Greatest
{
public static void main(String arg[])
{
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
System.out.println("Enter size of an Array from 2 to 20 ");
m=ob.nextInt( );
if(m>1 && m<=20)
{

Page 18 of 148
a=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{

a[i][j]=ob.nextInt( );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
}
}
}

Page 19 of 148
System.out.println(" The Greatest Number in matrix is "+max);
for(i=0;i<m;i++)
{
a[i][i]=max;
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+ );
}
System.out.println();
}
}
}

Page 20 of 148

Question 10:
Write a Program in Java to input a number and check whether it is a Kaprekar number or not.
Note: A positive whole number n that has d number of digits is squared and split into two pieces, a
right-hand piece that has d digits and a left-hand piece that has remaining d or d-1 digits.
If the sum of the two pieces is equal to the number, then n is a Kaprekar number. The first few
Kaprekar numbers are: 9, 45, 297 ..Example 1: 9
92 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number. Hence, 9 is a Kaprekar number.

Algorithm :
Step 1: Start
Step 2: Input a number from the user in the variable n.
Step 3: Pass the value of n to a function isKaprekar().
Step 4: The variable sq stores the square of the formal parameter num having the
value of n.

Page 21 of 148

## Step 5: Convert the variable sq to a string variable.

Step 6: If the square of a number is less than 9 concatenate it with 0.
Step 7: Calculate the length of string.
Step 8: The variable mid stores the middle index number of the string carrying the
number.
Step 9: String variable left contains the left part of the square and variable right
contains the right part of the square.
Step 10: Convert the variables left and right into int variables x and y.
Step 11: Variable sum stores the sum of variables x and y.
Step 12: The function returns the variable sum.
Step 13: In main method it is checked whether or not the sum is equal to the number
input from the user.
Step 14: If the condition is true , the number is called Kaprekar Number otherwise not.
Step 15: End.

Programming Code:
import java.util.*;
class VowelChar
{
public static void main(String arg[])
{
char a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
System.out.println("Enter size of an Array from 2 to 20 ");

Page 22 of 148
m=ob.nextInt( );
if(m>1 && m<=20)
{
a=new char[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{

a[i][j]=ob.nextLine().charAt(0 );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
char c=a[i][j];
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
{

Page 23 of 148
a[i][j]='*';
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}

Question 11:
Write a program to generate first 20 terms of the Fibonacci series in descending order in a
S.D.A of size 20. Store the numbers in another array which are primes.

Algorithm :
Step 1: Start
Step 2: Input a number from the user in the variable num.
Step 3: Copy the value of num into a variable z.

Page 24 of 148

## Step 4: Pass the value of num to a function reverse().

Step 5: Loop starts.
Step 6: Variable q extracts every last digit of variable q.
Step 7: Variable sum stores the digits in reverse order.
Step 8: The function returns the value of sum.

Step 9: In main method the compiler checks whether or not the value of sum is equal to the original numbe
or not.
Step 10: If the condition is true then the number is Palindrome Number otherwise not.
Step 11: End.

Page 25 of 148

Programming Code:
import java.util.*;
class Fibonacci
{
public static void main(String arg[])
{
int a[20];
int i, j, m;
Scanner ob=new Scanner(System.in);

for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
a[19]=1;a[18]=1;
int b[]=new int[20];
for(i=19;i>=2;i--)
{
a[i-2]=a[i]+a[i-1];
}
System.out.println("Fibonacci Series in descending order is :");
for(i=0;i<19;i++)

Page 26 of 148
System.out.print(a[i]+" ");
for(i=19;i>=0;i--)
{
int t=a[i];
for(j=2;j<t;j++)
{
if(t%j==0)
{
co++;
}
}
if(co==0)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<k;i++)
{
System.out.print(b[i]+" ");
}

}
}

Page 27 of 148

Question 12:
Write a program to declare a square matrix A[][] of order (M x M) where M must be greater than 3 and
less than 10. Allow the user to input positive integers into this matrix. Perform the following tasks on the
matrix:
(a) Sort the non-boundary elements in ascending order using any standard sorting technique and
rearrange
them
in
the
matrix.
(b)
Calculate
the
sum
of
both
the
diagonals.
(c) Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged matrix
with their sum.
Algorithm :
Programming Code :
import java.util.*;
class NonBoundrySorting
{
int A[][],B[],m,n;

void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the square matrix : ");

Page 28 of 148
m=sc.nextInt();
if(m<4 || m>10)
System.out.println("Invalid Range");
else
{
A = new int[m][m];
n = (m-2)*(m-2);
B = new int[n];

## System.out.println("Enter the elements of the Matrix : ");

for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter a value : ");
A[i][j]=sc.nextInt();
}
}
}
}

void convert(int s)
{
int x=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{

Page 29 of 148
if(i != 0 && j != 0 && i != m-1 && j != m-1)
{
if(s==1)
B[x] = A[i][j];
else
A[i][j] = B[x];
x++;
}
}
}
}

void sortArray()
{
int c = 0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(B[i]>B[j])
{
c = B[i];
B[i] = B[j];
B[j] = c;
}
}
}
}

Page 30 of 148

void printArray()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}

void printDiagonal()
{
int sum = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i==j || (i+j)==m-1)
{
System.out.print(A[i][j]+"\t");
sum = sum + A[i][j];
}
else
System.out.print("\t");
}

Page 31 of 148
System.out.println();
}
System.out.println("Sum of the Diagonal Elements : "+sum);
}

## public static void main(String args[])

{
NonBoundrySorting ob = new NonBoundrySorting();
ob.input();
System.out.println("*********************");
System.out.println("The original matrix:");
System.out.println("*********************");
ob.printArray();
ob.convert(1);
ob.sortArray();
ob.convert(2);

System.out.println("*********************");
System.out.println("The Rearranged matrix:");
System.out.println("*********************");
ob.printArray();
System.out.println("*********************");
System.out.println("The Diagonal Elements:");
System.out.println("*********************");
ob.printDiagonal();
}
}

Page 32 of 148

Write a program to input a 2-D array and check whether it is a Zero Matrix or not.
A zero matrix is a matrix (usually a square matrix) in which the entries outside the main diagonal () are all zero.
The diagonal entries themselves may or may not be zero (but all diagonal entries cannot be zero).
Algorithm :
Programming Code :
import java.util.*;
class ZeroMatrix
{
public static void main(String args[])throws Exception

Page 33 of 148
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int n=sc.nextInt();
int ar[][]=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{

ar[i][j]=sc.nextInt();
}
}
System.out.println("The Matrix is : ");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(ar[i][j]+"\t");
}
System.out.println();
}
int r=0, s=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i!=j && ar[i][j]!=0)

Page 34 of 148
{
r=1;
break;
}
if(i==j && ar[i][j]==0)
{
s++;
}
}
}
if(r==0 && s<n)
System.out.println("The matrix is Zero Matrix");
else
System.out.println("The matrix is not a Zero Matrix");
}
}

Page 35 of 148

Page 36 of 148

Question 13:
Write a program to input a number from the user and pass it to the function cube() and check whether
the number is Armstrong Number or not.
Sample Input : 153
Sample Output : Armstrong Number.

Algorithm :
Step 1: Start
Step 2: Input a number from the user in the variable num.
Step 3: Copy the value of num into a variable z.
Step 4: Pass the value of num to a function cube().
Step 5: Loop starts.
Step 6: The variable r extracts every last digit of the variable n(formal parameter).
Step 7: Variable d stores the sum of cubes of each digits of the number.
Step 8: The function returns the value of thye variable d.
Step 9: In main method the compiler checks whether the value of d is equal to the original number or
not.
Step 10: If the condition is true the number is called Armstrong Number otherwise not.
Step 11: End.

Page 37 of 148

Programming Code:
Write a program to input a 2-D matrix and find its Saddle Point.
A Saddle point is a point which is minimum in its row and maximum in its column.
Algorithm :
STEP 1 - START
STEP 2 - INPUT ELEMENTS IN THE ARRAY
STEP 3 - PRINT ARRAY
STEP 4 - START LOOP FROM i=0 , TO i<n , REPEAT STEP 5
SET min = A[i][j]
SET x = 0
STEP 5 - - START LOOP FROM j=0 , TO j<n , REPEAT STEP 6
STEP 6 - COMPARE IF (A[i][j]<min)
IF TRUE , SET min =A[i][j]
SET max = j
STEP 7 - SET max = A[0][x]
STEP 8 - START LOOP FROM k=0 , TO k<n , REPEAT STEP 9
STEP 9 - COMPARE IF (A[k][x]>max)
IF TRUE , SET max = A[k][x]
STEP 10 - CHECK IF (max==min)
IF TRUE , PRINT "Saddle point =" + max
SET f=1

Page 38 of 148
STEP 11 - if(f==0)
STEP 12 - END

Programming Code :
import java.io.*;
{
public static void main(String args[])throws IOException
{
System.out.print("Enter the order of the matrix : ");
int A[][]=new int[n][n];
System.out.println("Inputting the elements in the matrix");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
}
}
System.out.println("The Original Matrix is");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)

Page 39 of 148
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int max, min, x, f=0;
for(int i=0;i<n;i++)
{
min = A[i][0];
x = 0;
for(int j=0;j<n;j++)
{
if(A[i][j]<min)
{
min = A[i][j];
x = j;
}
}
max = A[0][x];
for(int k=0;k<n;k++)
{
if(A[k][x]>max)
{
max = A[k][x];
}
}
if(max==min)
{

Page 40 of 148
f=1;
}
}
if(f==0)
{
}
}
}

Page 41 of 148

Question 14:
Write a program to input a number and check whether it is a magic number or not. A number is said to
be a magic number if the sum of digits when added upto a single digit results in 1.
Example : 73 7+3=10 ; 1+0=1 . Therefore 73 is a Magic number

Algorithm :
Step 1: Start
Step 2: Input a number from the user in the variable n.
Step 3: Pass the value of n to a function isMagic().
Step 4: Loop starts.
Step 5: The loop finds whether the given number is a magic number or not and based upon this the
function returns true or false.
Step 6: In the main method the compiler checks the value of the Boolean variable containing the
result of the function.
Step 7: If its true the number is a Magic Number otherwise not.
Step 8: End.

Page 42 of 148

Programming Code:
Write a program to create a 4x4 matrix . Now swap the elements of 1 st row and 4th row. Display
the result i.e., interchange the elements of the 1 st row with 4th row.
Algorithm :
Programming Code :
import java.util.*;
public class Swapping
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[4][4];
int i,j,t;
System.out.println("Enter Array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
}
}

Page 43 of 148
System.out.println("Original Array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(j=0;j<4;j++)
{
t=a[0][j];
a[0][j]=a[3][j];
a[3][j]=t;
}
System.out.println("New Array after Swapping ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}

Page 44 of 148

Page 45 of 148

Question 15:
Algorithm :
Step 1: Start.
Step 2: Enter the full file path name in the variable 's' and find the index number of the last occurence
of '\' in the variable 'x'.
Step 3: Find the index number of the last occurence of '.' in the variable 'y'.
Step 4: Store the path name in the variable file (= s.substring(0,(x+1)).
Step 5: Store the file name in the variable file (= s.substring((x+1),y)
Step 6: Store the file name in the variable file (= s.substring((y+1)).
Step 7: Print all the answers.
Step 8: End.

Programming Code:
import java.io.*;
class ThreeWords
{
public static void main(String args[])throws IOException
{
System.out.print("Enter any word : ");

Page 46 of 148

int l = s.length();
int la=s.lastIndexOf(" ");
s1=s.substring(la);
s2=s.substring(0,la);
s3=s3+s1+" "+s2;
}
}
}

Question 16:

Write a program to input a word from the user and remove the duplicate characters present in it.
Example:
Sample Input :
Sample Output :

sushant
suhant

Algorithm :
Step 1: Start.
Step 2: Input a string from the user in the variable 's' and find its length in the variable 'l'.
Step 3: Declare a string 'ans' which is empty initially.
Step 4: Loop starts for(int i=0; i<l; i++)
Step 5: Variable 'ch' extracts every character of the string starting from index number '0'.
Step 6: The compiler checks whether the character is a blank space or not. If not then it
concatenates the character in the string 'ans'.
Step 7: Then it replaces the repeated ocurrences of the character by a blanks space.
s = s.replace(ch,' ');
Step 8: Loop breaks.
Step 9: Then it prints the string 'ans' containing the string s after removing the common characters.
Step 10: End.

Page 47 of 148

Programming Code:
import java.io.*;
class RemoveDupChar
{
public static void main(String args[])throws IOException
{
System.out.print("Enter any word : ");
int l = s.length();
char c =s.charAt(0);

Page 48 of 148

char ch=s.charAt(s.length()-1);
if(c==ch)
{
for(i=s.length()-1;i>=0;i--)
{
char cha=s.charAt(i);
w+=cha;
}
if(w.equals(s))
{
System.out.println("Word is Palindrome");
}
else
{
System.out.println("Word is only Special");
}
}
System.out.println("Word is not Special");

Page 49 of 148

Question 17:
Write a program to input m words in a S.D.A and print only those words that either start with a vowel
or end with a.

Algorithm:
Step 1: Start

Page 50 of 148

## Step 2: Input any string by the user.

Step 3: Convert the input string into lower case using toLowerCase() method. i.e. s=s.toLowerCase();
Step 4: Calculate the length of the string
.
Step 5: Start a loop from character a to z. i.e. for(char i='a'; i<='z'; i++)
Step 6: Initialize a counter variable count=0.
Step 7: Start a loop from 0 to length of the input string. i.e. for(int j=0; j<l; j++)
Step 8: Find character at each position of the string using charAt() method. i.e. ch=s.charAt(j)
Step 9: If the character extracted in step 8 is same that one in the outer loop then increment the
counter variable by 1. i.e. if(ch==i) count++;
Step 10: If the count is not equal to 0 then print the character and the count.
Step 11: At the end of the loop all characters in the string and their frequency will be printed.
Step 12: End

Programming Code:
import java.io.*;
class AlphabetFreq
{
public static void main(String args[])throws IOException
{
System.out.print("Enter any string: ");
int l=s.length();
String w="";

Page 51 of 148

for(i=0;i<m;i++)
{
String s=a[i];
char c=s.charAt(0);
char ch=s.charAt(s.length()-1);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||ch=='a')
w=w+s+" ";
}
System.out.println("All words either starting from vowels or ending with 'a' are ");
System.out.println(w);
}
}

Question 18:
Write a program to input a word and print it in alphabaetical order. Also print all the alphabets
between the first and last letter.
Sample Input: I am learning Java

Sample Output:
Shortest word = I
Length = 1
Longest word = learning
Length = 8

Algorithm:

Page 52 of 148

Step 1: Start
Step 2: Input any string by the user.
Step 3: Convert the input string into lower case using toLowerCase() method. i.e. s=s.toLowerCase();
Step 4: Calculate the length of the string.
Step 5: Start a loop from character a to z. i.e. for(char i='a'; i<='z'; i++)
Step 6: Initialize a counter variable count=0.
Step 7: Start a loop from 0 to length of the input string. i.e. for(int j=0; j<l; j++)
Step 8: Find character at each position of the string using charAt() method. i.e. ch=s.charAt(j)
Step 9: If the character extracted in step 8 is same that one in the outer loop then increment the
counter variable by 1. i.e. if(ch==i) count++;
Step 10: If the count is not equal to 0 then print the character and the count.
Step 11: At the end of the loop all characters in the string and their frequency will be printed.
Step 12: End

Programming Code:
import java.io.*;
class Short_long_word

Page 53 of 148

{
public static void main(String args[])throws IOException
{
System.out.print("Enter any sentence : ");
s=s+" ";
s=s.toLowerCase();
for(i=97;i<=122;i++)
{
for(j=0;j<s.length();j++)
{
if(i==s.charAt(i))
{
w+=s.charAt(i);
}
}
}
System.out.println(w);
char c=w.charAt(0);
char ch=w.charAt(w.length()-1);
for(i=c;i<=ch;i++)
{
System.out.print((char)(i)+" ");
}
}
}

Page 54 of 148

Question 19:
Write a program to input a name containg three words and print the output with the initials and the
surname.
Sample Input: Santosh Kumar Shukla
Sample Output:S.K Shukla

Algorithm:
Step 1: Start
Step 2: Input a string by the user.
Step 3: Calculate the length of the string.
Step 4: Find the first letter of the string using charAt() method and concatenate in a variable initialized
to null initially. i.e. st1=st1+st.charAt(0);
Step 5: Start a loop from 0 to length of the string. i.e. for(int i=0;i<x;i++)
Step 6: Find the character at ith position in the loop.
Step 7: If the character extracted in step 6 is blank then increment a counter variable initialized to 0
initially. i.e. d=d+1
Step 8: If the value of the counter variable d==1 then at the end of the concatenated
variable containing first letter in step 4 append a dot . and the next character after blank i.e. the
character at (i+1)th position i.e. st1=st1+"."+(st.charAt(i+1))
Step 9: If the value of the counter variable d==2 then at the end of the concatenatedvariable
containing first letter in step 4 append a dot . and the remaining string after the second occurance of
blank using substring method. i.e. st1=st1+"."+(st.substring(i,x))
Step 10: At the end of the loop the concatenated string represents the output.
Step 11: End

Programming Code:
import java.io.*;

Page 55 of 148

## public class Surname

{
public static void main(String args[])throws IOException
{
String st,st1=" "; int x,y,d=0;
char b=' ';
System.out.println("Enter a String");
x=st.length();
st1=st1+st.charAt(0);
System.out.println("The shortform of the name with the surname is");
for(int i=0;i<x;i++)
{
b=st.charAt(i);
if(b==' ')
{
d=d+1;
if(d==1)
st1=st1+"."+(st.charAt(i+1));
if(d==2)
st1=st1+"."+(st.substring(i,x));
}
}
System.out.println(st1);
}

Page 56 of 148

Page 57 of 148

Question 20:
Write a program to accept a string and print the string in reverse order.
Sample Input: wood
Sample Output: doow

Algorithm:
Step 1: Start
Step 2: Input a string by the user.
Step 3: Append a blank character at the end of the string.
Step 4: Calculate the length of the string. i.e. x=st1.length()
Step 5: Start a reverse loop from the length of the string to 0. i.e. for(y=--x;y>=0;y--)
Step 6: Find the character at each yth position inside the loop and concatenate it in a variable
initialized to null initally.
Step 7: When the loop ends the concatenated variable will represent the reverse string.
Step 8: Print the reverse string.

Page 58 of 148

Step 9: End

Programming Code:
import java.io.*;
public class StringReverse
{
public static void main(String args[])throws IOException
{
System.out.println("Enter a String");

Page 59 of 148

String w="",maxw="";int max=0,le=0,co=0;
for(i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c!=' ')
{
w+=c;
}
else
{
co=0;
for(j=0;j<w.length();j++)
{
char ch=w.charAt(j);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
co++;
}
}
if(co>max)
{
maxw=w;
max=co;
}
}
}
System.out.println("Word with maximum number of vowels is "+maxw);
}
}

Question 21:
Write a program to input a Sentence from the user and print the number of 2 letters 3 letters and 4
letters words.

Page 60 of 148

Algorithm :
Step 1: Start.
Step 2: Input a String from the user in the variable 's'.
Step 3: Input the letter which has to be replaced in the original word in the variable 'c'.
Step 4: Input the letter which has to be replaced in the original word in place of the character in
variable c in the variable 'ch'.
Step 5: Replace the variable c with ch through the function s=s.replace(c,ch);
Step 6: Loop starts.
Step 7: Variable cha extracts the characters of the string s starting from index number '0'.
Step 8: If the value of cha is equal to either 'a' or 'e' or 'i' or 'o' or 'u' or 'A' or 'E' or 'I' or 'O' or 'U' then
the loop breaks.
Step 9: Then the compiler prints the piglatin string through (s.substring(i)+s.substring(0,i)+"ay");
Step 10: End.

Programming Code:
import java.util.*;
public class Replace
{
public static void main(String args[])
{

Page 61 of 148

## Scanner sc=new Scanner(System.in);

System.out.println("Enter A Sentence ");
String s=sc.nextLine();
String w="",maxw="";int max=0,le=0,l1=0,l2=0,l3=0;
for(i=0;i<s.length();i++)
{
char c=s.charAt(i);

if(c!=' ')
{
w+=c;
}
else
{
int le=w.length();
if(le==2)
l1++;
if(le==3)
l2++;
if(le==4)
l3++;
}
{
maxw=w;
max=le;
}
}

Page 62 of 148

}
System.out.println("Number of 2 letter words are "+l1);
System.out.println("Number of 3 letter words are "+l2);
System.out.println("Number of 4 letter words are "+l3);
}
}

Question 22:
Write a program to input a sentence in lower case and upper case both print the new string after
deleting all the Upper case letters of the String. The program also prints the number of deleted Upper
case letters

Page 63 of 148

## Sample Input : Unerstanding Computer Applications Class X

Sample Output : nderstanding omputer pplications lass
Number of deleted Uppercase Letters : 5

Algorithm:
Step 1: Start
Step 2: Input a sentence by the user
Step 3: Start a loop from 0 to length of the string. i.e. for(i=0;i<s.length();i++)
Step 4: Find the character at ith position in the string using charAt function. i.e. char ch=s.charAt(i)
Step 5: Check whether the extracted character is of upper case.
Step 6: If the character is not of upper case then concatenate the character in a variable initialized to
null initially. i.e. st+=ch
Step 7: If the character is not of upper case then increment a counter variable initialized to zero
initially.
Step 8: When the loop ends the concatenated string will be the new string after deleting upper case
characters.
Step 9: The counter variable will be the number of upper case characters deleted from the input
sentence.
Step 10: End

Programming Code:
import java.io.*;
class RemoveWord
{
public static void main(String args[])throws IOException
{

## System.out.print("Enter any sentence : ");

Page 64 of 148

s = s.toUpperCase();
int l = s.length();

String ans="";
char last = s.charAt(l-1);

## if(last == '.' || last == '?' || last == '!')

{
String word[]=s.split("[.?! ]+");
int c = word.length;

## System.out.print("Enter the word to delete : ");

del = del.toUpperCase();

## System.out.print("Enter the word position in the sentence : ");

if(x<1 || x>c)
{
System.out.println("Sorry! The word position entered is out of range");
}

else
{
for(int i=0; i<c; i++)
{

## if(word[i].equals(del)==true && i == x-1)

continue;
ans = ans + word[i] + " ";
}
System.out.print("Output : "+ans.trim()+last);
}
}

else
{
System.out.println("Invalid Input. End a sentence with either '.', '?' or '!'");

Page 65 of 148

}
}
}

Question 23:
Write a program to input a three letter word and display all the anagrams

Algorithm:
Step 1: Start
Step 2: Input a three letter word from the user.
Step 3: Calculate the length of the word.

Page 66 of 148

Step 4: Start three nested loops i, j and k from 0 to length of the word.
Step 5: When the value of the loop variables I, j and k is not equal then print the concatenated value
of the characters at ith, jth and kth position. i.e. str.charAt(i)+""+str.charAt(j)+""+str.charAt(k).
Step 6: At the end of the loop all the anagrams of the three letter word will be printed.
Step 7: End

Programming Code:
import java.io.*;
public class CombinationOfLetters
{
public static void main(String args[])throws IOException
{

Page 67 of 148

String str;
int i,j,k,l;
char chr;
System.out.println("Enter a letter word");
l=str.length();
for(i=0;i<l;i++)
{
for(j=0;j<l;j++)
{
for(k=0;k<l;k++)
{
if(i!=j && j!=k && k!=i)
System.out.println(str.charAt(i)+""+str.charAt(j)+""+str.charAt(k));
}

}
}
}
}

Page 68 of 148

Question 24:
Print two string such that there are characters of even position of 1st string and odd position characters of 2nd
string. However the loop should run till the length of the shorter string

Algorithm:
Step 1: Start
Step 2: Input the two strings from the user.

Page 69 of 148

## Step 3: Calculate the length of both strings separately.

Step 4: Find out which length is less i.e. which string is shorter using min e.g function.Math.min(l1,l2).
Step 3: Loop till the end of length of the shorter string. Loop will be on interval of +2. i.e.
for(i=0;i<l;i+=2) . This is to find out 1 st, 3rd characters and so on of the first string (i.e. odd positioned
characters).
Step 4: Find character at the postion of the loop of the first string. e.g
chr1=str1.charAt(i);
Step 5: Concatenate and store this character in a new variable. e.g. str3=str3+chr1;
Step 6: Find character at the position+1 of the loop of the second string. e.g chr2=str 2.charAt(i+1)
Step 5: Concatenate and store this character in the same variable str3=str3+chr2
Step 6: When the loop ends this concatenated variable will contain the output string.
Step 7: End.

Programming Code:
import java.io.*;
public class Frame
{
public static void main(String args[])throws IOException

Page 70 of 148

{
String str1,str2,str3=" ";
int i,j,l;
char chr1,chr2;
System.out.println("Enter first String");
System.out.println("Enter second String");
int l1=str1.length();
int l2=str2.length();
l=Math.min(l1,l2);
for(i=0;i<l;i+=2)
{
chr1=str1.charAt(i);
str3=str3+chr1;
chr2=str2.charAt(i+1);
str3=str3+chr2;
}
System.out.println("The new String");
System.out.println(str3);
}
}

Page 71 of 148

Question 25:
Write a program to input two strings and swap them without using a third variable.

Page 72 of 148

Algorithm:
Step 1: Start
Step 2: Input two strings to be swapped from the user (s1,s2).
Step 3: Calculate the length of the first string (len1).
Step 4: Print the strings before swapping.
Step 5: Concatenate both the string and store the concatenated value in the first input string variable.
i.e. s1=s1+s2.
Step 6: Now the string s2 will be the s1.substring(0,len1) and string s1 will be s1.substring(len1).
Step 7: End

Programming Code:
import java.io.*;
class Swap_Strings
{
public static void main(String args[])throws IOException

Page 73 of 148

{
System.out.print("Enter the 1st String : ");
int len1=s1.length();
System.out.print("Enter the 2nd String : ");
System.out.println("-------------------------------");
System.out.println("Strings Before Swapping : ");
System.out.println("1st String = "+s1);
System.out.println("2nd String = "+s2);
s1=s1+s2;
s2=s1.substring(0,len1);
s1=s1.substring(len1);
System.out.println("-------------------------------");
System.out.println("Strings After Swapping : ");
System.out.println("1st String = "+s1);
System.out.println("2nd String = "+s2);
}
}

Page 74 of 148

Question 26:

Page 75 of 148

Question 27:
Write a program to input a String from the user and print the String in alphabetical order.

Algorithm:
Step 1: Start
Step 2: Input the string from the user.
Step 3: Calculate the length of the input string.
Step 4: Start first loop from 65 to 90 (range of ASCII codes from A to Z in alphabetical order) i.e.
for(i=65;i<=90;i++)
Step 5: Start second loop from the 1 to end of the string. i.e. for(j=0;j<p;j++)

Page 76 of 148

Step 6: Find the character at each position of the length of the string. i.e. chr=str.charAt(j);
Step 7: Check if the character is the same to the character in the first loop when the ascii code is
converted to character or the same to the capital character when converted. i.e. if(chr==(char)i||
chr==(char)(i+32))
Step 8: If the character is same as checked in step 7 then print the character.
Step 9: When the loop ends the characters in the input string will be printed in alphabetical order.
Step 10: End

Programming Code:

import java.io.*;
public class AlphabeticalOrder
{
public static void main(String args[])throws IOException
{

Page 77 of 148

String str;
int i,j,p;
char chr;
p=str.length();
for(i=65;i<=90;i++)
{
for(j=0;j<p;j++)
{
chr=str.charAt(j);
if(chr==(char)i||chr==(char)(i+32))
System.out.println(chr);
}
}
}
}

Page 78 of 148

Question 28:

Page 79 of 148

Question 29:
Write a program to input a String from the user and check whether it is a palindrome string or not.
Sample Output: It is a palindrome string.

Algorithm :
Step 1: Start
Step 2: Input a string by the user.
Step 3: Calculate the length of the input string. i.e. p=word.length();
Step 4: Start a reverse loop from length 1 to 0. i.e. for(i=p-1;i>=0;i--)

Page 80 of 148

Step 5: Find the character at each position of the loop in the string. i.e. chr=word.charAt(i);
Step 6: Concatenate the character extracted in step 5 in a variable initialized to null initially.
Step 7: After the end of the loop compare the input word with the final concatenated word.
Step 8: If both the words the input word and concatenated word is same then the string is palindrome
else it is not palindrome.
Step 8: End.

Programming Code:
import java.io.*;
public class PalindromeString
{
public static void main(String args[])throws IOException
{
String word1="";

Page 81 of 148

int i,p;
char chr;
p=word.length();
for(i=p-1;i>=0;i--)
{
chr=word.charAt(i);
word1=word1+chr;
}
if(word.equals(word1))
System.out.println("The String is a Palindrome String");
else
System.out.println("The String is not a palindrome String");
}
}

Page 82 of 148

Question 30:
Write a program to input two words from the user and print the new strings after removing all the common
characters from the two strings.
Sample Input: sushant
Sample Output: suhant

Algorithm :
Step 1: Start
Step 2: Input the two strings from the user.
Step 3: Loop till the end of length of the first string.
Step 4: Find character at each position of the first string.
Step 5: Check the occurance of the character in the other string using indexOf function eg:
st.indexOf(c)
Step 6: If the character does not occur in the second string the above function will return the value -1.
i.e. st.indexOf(c)==-1

Page 83 of 148

Step 7: If st.indexOf(c)==-1 then concatenate the character in a varaiable and store. i.e. ans += c.
Step 8: When the loop ends this concatenated value will be the characters of first string which do not
occur in the second string i.e. the non common characters.
Step 9: Repeat Step 1 to Step 7 for string 2 and compare with string 1 taking a loop till the end of
second string.
Step 10: When the loop ends this concatenated value will be the characters of second string which do
not occur in the first string i.e. the non common characters.
Step 11: End

Programming Code:
import java.util.*;
public class RemoveCommonCharacters
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String st=sc.nextLine();
String ans="";
for(int i=0;i<s.length();i++)
{

Page 84 of 148

char c=s.charAt(i);
if(st.indexOf(c)==-1)
ans += c;

}
System.out.print("Non-Common Characters in 1st String are :");
System.out.print(ans);
System.out.println();
ans="";
for(int i=0;i<st.length();i++)
{
char c=st.charAt(i);
if(s.indexOf(c)==-1)
ans +=c;

}
System.out.print("Non-Common Characters in 2nd String are :");
System.out.print(ans);
}
}

Page 85 of 148

Question 31.
Write a program to display Pascal's Triangle.

ALGORITHM
STEP 1 - START
STEP 2 - pas[0] = 1
STEP 3 - IF i=0 THEN GO TO STEP 4
STEP 4 - IF j=0 THEN GO TO STEP 5
STEP 5 - PRINT pas[j]+" "
STEP 6 - i++& IF i<=i GO TO STEP 5
STEP 8 - IF j=i+1 THEN GO TO STEP 7
STEP 9 - pas[j]=pas[j]+pas[j-1]
STEP 10 - j--& IF j>0 GO TO STEP 9
STEP 11 END

Page 86 of 148

Programming Code :
import java.util.*;
class Pascal
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int [ ] pas = new int [n+1];
pas[0] = 1;
for (int i=0; i<n; i++)
{
for (int j=0; j<=i; ++j)

Page 87 of 148
System.out.print(pas[j]+" ");
System.out.println( );
for (int j=i+1; j>0; j--)
pas[j]=pas[j]+pas[j-1];
}
}
}

Page 88 of 148

Question 32.
Write a program to enter a 2-D array and check whether it is a magic square or not.

Algorithm :
Programming Code :
import java.util.*;
class MagicSquare
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the dimension of magical square=");
int n = sc.nextInt();
int arr[][]=new int[n][n],c=n/2-1,r=1,num;
for(num=1;num<=n*n;num++)
{
r--;

Page 89 of 148
c++;
if(r==-1)
r=n-1;
if(c>n-1)
c=0;
if(arr[r][c]!=0)
{
r=r+2;
c--;
}
arr[r][c]=num;
if(r==0&&c==0)
{
r=n-1;
c=1;
arr[r][c]=++num;
}
if(c==n-1&&r==0)
arr[++r][c]=++num;
}
System.out.println("Magic Matrix");
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
System.out.print(arr[r][c]+" ");
System.out.println();
}
}

Page 90 of 148
}

Page 91 of 148

Question 33.
Write a program to input a 5X5 matrix and find its sum.
Algorithm :
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM x =0 to x<5 REPEAT STEP 4
STEP 4 - FROM y=0 TO y<5 REPEAT STEP 5
STEP 5 - PRINT (a[x][y]+" "
STEP 6 - FROM x =0 to <5 REPEAT STEP 7
STEP 7 - FROM Y=0 TO Y<5 REPEAT STEP 8
STEP 8 - Sum+=Sum+a[x][y]
STEP 9 - PRINT Sum
STEP 10 - END.

Page 92 of 148

Programming Code :
import java.util.*;
class MatrixSum
{
public static void main(String args[])
{
int a[][]=new int[5][5];
Scanner sc=new Scanner(System.in);
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<5;x++)
{
for(y=0;y<5;y++)
{
a[x][y]=sc.nextInt();
}
}
System.out.println("Original Array");
for(x=0;x<5;x++)
{
for(y=0;y<5;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.println();
}
for(x=0;x<5;x++)

Page 93 of 148
{
for(y=0;y<5;y++)
{
Sum=Sum+a[x][y];
}
}
System.out.println("Sum of array elements "+Sum);
}
}

Page 94 of 148

Page 95 of 148

Question 34.
Write a program to input a 2-D array and find the sum of each column.
Algorithm :

STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM x =0 to x<4 REPEAT STEP 4
STEP 4 - FROM y =0 to y<4 REPEAT STEP 5
STEP 5 - PRINT (a[x][y]+" "
STEP 6 - FROM x =0 to x<4 REPEAT STEP 7 , STEP 9 and STEP 10
STEP 7 - FROM y =0 to y<4 REPEAT STEP 8
STEP 8 - Sum=Sum+a[x][y] ,
STEP 9 - PRINT Sum
STEP 10 - Sum = 0
STEP11 END

Page 96 of 148

Programming Code :
import java.util.*;
class ColoumnSum
{
public static void main(String args[])
{
int a[][]=new int[4][4];
Scanner sc=new Scanner(System.in);
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
a[x][y]=sc.nextInt();
}
}
System.out.println("Array");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.println();

Page 97 of 148
}
for(y=0;y<4;y++)
{
for(x=0;x<4;x++)
{
Sum=Sum+a[x][y];
}
System.out.println("Sum of Column "+(y+1)+" is "+Sum);
Sum=0;
}
}
}

Page 98 of 148

Question 35.
Write a program to input a 2-D array and find the sum of diagonal.
Algorithm :

STEP 1- START
STEP 2- INPUT a[]
STEP 3- FROM x =0 to x<4 REPEAT STEP 4
STEP 4- FROM y =0 to y<4 REPEAT STEP 5
STEP 5- PRINT (a[x][y]+" "
STEP 6- FROM x =0 to x<4 REPEAT STEP 7
STEP 7 - Sum=Sum+a[x][y] , y=y+1
STEP 8 - FROM x =0 to x<4 REPEAT STEP 9
STEP 9 - FROM y =0 to y<4 REPEAT STEP 10
STEP 10 - CHECK if((x+y)==3)
STEP 11 - sum+=a[x][y]
STEP 9- PRINT Sum AND sum
STEP 10 - Sum = 0
STEP11- END

Page 99 of 148

Programming Code :
import java.util.*;
class DiagonalSum
{
public static void main(String args[])
{
int a[][]=new int[4][4];
Scanner sc=new Scanner(System.in);
int x,y,z,Sum=0,sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
a[x][y]=sc.nextInt();
}
}
System.out.println("Array -");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{

## Page 100 of 148

System.out.print(a[x][y]+" ");
}
System.out.println();
}
y=0;
for(x=0;x<4;x++)
{
Sum=Sum+a[x][y];
y=y+1;
}
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
if((x+y)==3)
{
sum+=a[x][y];
}
}
}
System.out.println("Sum of Right diagonal is "+Sum);
System.out.println("Sum of Left diagonal is "+sum);
Sum=0;
}
}

## Page 102 of 148

Question 36.
Write a program to declare a matrix A[ ][ ] of order (MxN) where M is the
number of rows and N is the number of columns such that both M and N must
be greater than 2 and less than 20. Allow the user to input integers into this
matrix. Perform the following tasks on the matrix:
a) Display the input matrix.
b) Find the maximum and minimum value in the matrix and display them along
with their positions.
c) Sort the elements of the matrix in ascending order using any standard sorting
technique and rearrange them in the matrix.
d) Output the rearranged matrix.
Algorithm :
STEP 1 - ACCEPT THE DIMENSION OF THE ARRAY AND CHECK IF IT IS IN THE RANGE.
STEP 2 - IF DIMENSION SATISFIES THE RANGE, CREATE AN ARRAY OF SIZE MXN AND
ACCEPT THE ELEMENTS.
STEP 3 - ADD THE ELEMENTS OF THE TWO DIMENSIONAL ARRAY TO A SINGLE
DIMENSIONAL ARRAY.
STEP 4 - SORT THE ELEMENTS OF THE 1-D ARRAY IN ASCENDING ORDER USING BUBBLE
SORT TECHNIQUE.
STEP 5 - FIND THE FIRST ELEMENT OF THE SORTED ARRAY AND FIND ITS POSITION IN
THE 2-D ARRAY. THIS IS OUR SMALLEST ELEMENT.
STEP 6 - FIND THE LAST ELEMENT OF THE SORTED ARRAY AND FIND ITS POSITION IN
THE 2-D ARRAY. THIS IS OUR LARGEST ELEMENT.
STEP 7 - PRINT THE SORTED ARRAY IN A 2X2 MATRIX FORM.
STEP 8 - END

## Page 103 of 148

Programming Code :
import java.util.*;
class Matrix_Sort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the dimensions of the matrix");
int M=sc.nextInt();
int N=sc.nextInt();
if(M<=2||M>=20||N<=2||M>=20)
System.out.println("SIZE OUT OF RANGE");
else
{
int A[][]=new int[M][N];
System.out.println("Enter the elements of matrix");
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("ORIGINAL MATRIX:");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int a[]=new int[M*N];
int k=0,tmp;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
a[k]=A[i][j];
k++;
}

## Page 104 of 148

}
for(i=0;i<k;i++)
{
for(j=0;j<k-1-i;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
int max=a[k-1], min=a[0];
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
if(A[i][j]==max)
{
System.out.println("LARGEST NUMBER\t:"+max);
System.out.println("\t\tROW= "+i+"\n\t\tCOLUMN= "+j);
}
if(A[i][j]==min)
{
System.out.println("SMALLEST NUMBER\t:"+A[0][0]);
System.out.println("\t\tROW= "+i+"\n\t\tCOLUMN= "+j);
}
}
}
System.out.println("REARRANGED MATRIX");
k=0;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
System.out.print(a[k]+"\t");
k++;
}
System.out.println();
}
}
}

## Page 106 of 148

Question 37
Write a program to declare a square matrix A[ ] [ ] of order MxM where M is the
number of rows and columns, such that M must be greater than 2 and less than
10. Accept the value of M as user input. Display an appropriate message for an
invalid input. Allow the user to input integers into this matrix.
a) Display the original matrix.
b) Check if the given matrix is symmetric or not.
A square matrix is said to be Symmetric if the element of the ith row and
jth column is equal to element of jth row and Ith column.
c) Also find the sum of left and right diagonals .
Algorithm :

## Algorithm for void main()

STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP

1:
2:
3:
4:
5:
6:
7:
8:

START.
CREATE A CLASS OBJECT, OBJ.
CALL A FUNCTION VOID ACCEPT().
CALL A FUNCTION VOID DISPLAY_ORIGINAL().
CALL A FUNCTION VOID CHECKSYMMETRIC().
CALL A FUNCTION VOID COMPUTE().
END.

STEP
STEP
STEP
STEP

1:
2:
3:
4:

## IF M>2&&M<10 THEN GOTO STEP 2.

RUN A FOR LOOP FROM I0 TO M AND A NESTED LOOP FROM J0 TO M AND REPEAT STEP 3.
A[I][J]SC.NEXTINT().
ELSE PRINT "THE MATRIX SIZE IS OUT OF RANGE" AND EXIT THE PROGRAM.

STEP
STEP
STEP
STEP

1:
2:
3:
4:

## PRINT "ORIGINAL MATRIX".

RUN A FOR LOOP FROM I0 TO M AND A NESTED LOOP FROM J0 TO M.
PRINT A[I][J]+ .
END LOOP.

## Algorithm for void checkSymmetric()

STEP
STEP
STEP
STEP
STEP

1:
2:
3:
4:
5:

FLAG1.
RUN A FOR LOOP FROM I0 TO M AND A NESTED LOOP FROM J0 TO M AND REPEAT STEP 3.
IF A[I][J]!=A[J][I] THEN FLAG0.
IF FLAG=1 THEN PRINT "THE GIVEN MATRIX IS SYMMETRIC".
ELSE PRINT "THE GIVEN MATRIX IS NOT SYMMETRIC".

## Algorithm for void compute()

STEP
STEP
STEP
STEP
STEP
STEP

1:
2:
3:
4:
5:
5:

SUMLD0, SUMRD0.
RUN A FOR LOOP FROM I0 TO M AND A NESTED LOOP FROM J0 TO M AND REPEAT STEPS .
IF I=J THEN SUMLDSUMLD+A[I][J].
IF I+J=M THEN SUMRDSUMRD+A[I][J].
PRINT SUMLD.
PRINT SUMRD.

Programming Code :
import java.util.Scanner;
class Symmetric
{ int A[][];
int M;
Symmetric(int size)
{ M=size;
A=new int[M][M];
}
public void accept()
{ if(M>2&&M<10)
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter elements of the Matrix");
for(int i=0;i<M;i++)
{ for(int j=0;j<M;j++)
A[i][j]=sc.nextInt();
}
}
else
{ System.out.println("THE MATRIX SIZE IS OUT OF RANGE");
System.exit(0);
}
}

## Page 108 of 148

public void display_Original()
{ System.out.println("\nORIGINAL MATRIX");
for(int i=0;i<M;i++)
{ for(int j=0;j<M;j++)
System.out.print(A[i][j]+" ");
System.out.println();
}
}
public void checkSymmetric()
{ int flag=1;
for(int i=0;i<M;i++)
{ for(int j=0;j<M;j++)
{ if(A[i][j]!=A[j][i])
{ flag=0;
break;
}
}
}
if(flag==1)
System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
else
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
}
public void compute()
{ int sumLD=0,sumRD=0;
for(int i=0;i<M;i++)
{ for(int j=0;j<M;j++)
{ if(i==j)

## Page 109 of 148

sumLD+=A[i][j];
if((i+j)==M)
sumRD+=A[i][j];
}
}
System.out.println("The sum of the left diagonal= "+sumLD);
System.out.println("The sum of the right diagonal= "+sumRD);
}
public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
System.out.print("Enter size of Matrix: ");
int size=sc.nextInt();
Symmetric obj=new Symmetric(size);
obj.accept();
obj.display_Original();
obj.checkSymmetric();
obj.compute();
}
}

## Page 111 of 148

Question 38.
Write a program to create a m x n matrix and print the prime elements in it
along with the row and column in which they are present.
Algorithm :

## STEP-1: INPUT THE NUMBER OF ROWS AND COLUMNS AS M AND N

STEP-2: CREATE A M X N MATRIX
STEP-3: ENTER M X N ELEMENTS IN THE MATRIX
STEP-4: RUN A LOOP FOR THE ROWS M
STEP-5: RUN A LOOP FOR THE COLUMNS N
STEP-6: PRINT ORIGINAL MATRIX AND RUN A LOOP TO FIND THE NUMBER OF FACTORS OF
THE MATRIX ELEMENT
STEP-7: IF THE NUMBER OF FACTORS IS 2 PRINT THE ELEMENT, ITS ROW INDEX AND
COLUMN INDEX
STEP-8: CONTINUE PROCESS FOR ALL THE ELEMENTS
STEP-9: END

## Page 112 of 148

Programming Code :
import java.util.*;
public class searchPrime
{
public static void main(String args[])throws InputMismatchException
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows and columns of a matrix: ");
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
int i,j,k,s=0;
System.out.println("Enter "+(m*n)+" elements:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();

## Page 113 of 148

}
System.out.println("Prime Element\t Row\t Column");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
int c=0;
for(k=1;k<=a[i][j];k++)
{
if(a[i][j]%k==0)
{
c++;
}
}
if(c==2){
System.out.println(a[i][j]+"\t\t"+i+"\t\t"+j);
}
}
}
}
}

## Page 115 of 148

Question 39.
Write a program to sort the border elements of a n x n matrix in ascending order.
Algorithm :

## STEP-1: INPUT THE NUMBER OF ROWS FOR A SQUARE MATRIX

STEP-2: CREATE AN N X N MATRIX AND ENTER ELEMENTS IN IT
STEP-3: RUN TWO LOOPS TO ACCESS THE MATRIX
STEP-4: CHECK FOR BORDER ELEMENTS
STEP-5: IF FOUND, STORE THEM IN A SINGLE DIMENSION ARRAY, SAY B[]
STEP-6: CONTINUE THE PROCESS TILL ALL THE BORDER ELEMENTS ARE STORED IN B[]
STEP-7: NOW, SORT THE ARRAY B[] IN ASCENDING ORDER USING ANY SORTING
TECHNIQUE
STEP-8: TO STORE THE SORTED BORDER ELEMENTS IN THE MATRIX, RUN A LOOP FOR
THE
FIRST ROW AND STORE ELEMENTS FROM B[] IN THE MATRIX
STEP-9: RUN A LOOP FOR LAST COLUMN AND DO THE SAME
STEP-10: RUN A LOOP FOR LAST ROW AND THEN FIRST COLUMN TO STORE THE
ELEMENTS FROM B[] TO MATRIX
STEP-11: FINALLY, PRINT THE MATRIX THAT NOW HAS BORDER ELEMENTS SORTED IN
ASCENDING ORDER.
STEP-12: END

## Page 116 of 148

Programming Code :
import java.util.*;
public class BoundrySorting
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows for a sqaure matrix: ");
int n=sc.nextInt();
int a[][]=new int[n][n];
int i,j,s=0;
System.out.println("Enter "+(n*n)+" elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();

## Page 117 of 148

}
int b[]=new int[n*n];
int x=0;

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==0 || j==0 || i==n-1 || j==n-1)
{
b[x++]=a[i][j];
}
}
}

for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(b[i]>b[j])
{
int t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}

## Page 118 of 148

int c=0;
for(i=0;i<n;i++)
a[0][i]=b[c++];
for(i=1;i<n;i++)
a[i][n-1]=b[c++];
for(i=n-2;i>=0;i--)
a[n-1][i]=b[c++];
for(i=n-2;i>0;i--)
a[i][0]=b[c++];
System.out.println("Sorted Array");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ") ;
}
System.out.println();
}
}
}

## Page 120 of 148

Question 40.
Write a program to print the sum of border elements of an n x n matrix.
Algorithm :

## STEP-1: INPUT THE NUMBER OF ROWS N FOR A SQUARE MATRIX

STEP-2: CREATE AN NXN MATRIX
STEP-3: ENTER NXN ELEMENTS FROM THE USER
STEP-4: RUN A LOOP FOR THE NUMBER OF ROWS
STEP-5: RUN A LOOP FOR THE NUMBER OF COLUMNS
STEP-6: IF ROW IS FIRST OR LAST OR IF COLUMN IF FIRST OR LAST ADD THE ELEMENT
STEP-7: CONTINUE THE PROCESS TILL THE ENTIRE MATRIX IS CHECKED
STEP-8: DISPLAY THE SUM
STEP-9: END

Programming Code :

## Page 121 of 148

import java.util.*;
public class BoundrySum
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows for a sqaure matrix: ");
int n=sc.nextInt();
int a[][]=new int[n][n];
int i,j,s=0;
System.out.println("Enter "+(n*n)+" elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++){
a[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
for(i=0;i<n;i++)
{

## Page 122 of 148

for(j=0;j<n;j++)
{
if(i==0 || j==0 || i==n-1 || j==n-1){
s=s+a[i][j];
}
}
}
System.out.println("Sum of border elements: "+s);
}
}

## Page 123 of 148

Question 41
Write a program to input 2 double dimensional arrays and find their product.
Algorithm
STEP 1 - START

## Page 124 of 148

STEP 2 - DECLARE THREE ARRAYS m[][] n[][] p[][] OF ORDER 4x4
STEP 3 - INPUT ELEMENTS IN ARRAYS m[][] AND n[][]
STEP 4 - DISPLAY BOTH ARRAYS.
STEP 5 - START LOOP FROM i=0 , TO i < 4 , REPEAT STEP 6.
STEP 6 - START LOOP FROM j=0 , TO j < 4 , REPEAT STEP 7.
STEP 7 - SET p[i][j]=0
STEP 8 - START LOOP FROM k=0 , TO k < 4 REPEAT STEP 9.
STEP 9 - SET p[i][j] = p[i][j]+m[i][k]*n[k][j]
STEP 10 - PRINT ARRAY p[][]
STEP 11 - END.

Programming Code
import java.util.*;
public class Multiplication
{
public static void main(String args[])

## Page 125 of 148

{
Scanner sc=new Scanner(System.in);
int i,j,k;
int m[][]=new int[4][4];
int n[][]=new int[4][4];
int p[][]=new int[4][4];
System.out.println("Enter elements in the first array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
m[i][j]=sc.nextInt();
}
}
System.out.println("Enter elements in the second array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
n[i][j]=sc.nextInt();
}
}
System.out.println("First Array ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");

## Page 126 of 148

}
System.out.println();
}
System.out.println("Second Array ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(n[i][j]+" ");
}
System.out.println();
}
System.out.println("Product Of Arrays");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
p[i][j]=0;
for(k=0;k<4;k++)
{
p[i][j]+=m[i][k]*n[k][j];
}
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{

## Page 127 of 148

System.out.print(p[i][j]+" ");
}
System.out.println();
}
}
}

## Page 128 of 148

Question 42.
Write a program to input a 2-D array and print its transposed matrix.
Algorithm
STEP 1 - START
STEP 2 - INPUT SIZE OF ARRAY
STEP 3 - INPUT ELEMENTS IN ARRAY a[][]
STEP 4 - PRINT ORIGINAL ARRAY.
STEP 5 - START LOOP FROM i=0 , i<n , REPEAT STEP 7.
STEP 6 - START LOOP FROM j= i +1 , TO j < n REPEAT STEP 7.
STEP 7 - SET

## Page 129 of 148

temp = a[i][j];
a[i][j] = a[j][i]
a[j][i] = temp
STEP 8 - PRINT NEW ARRAY a[][]
STEP 9 - END

Programming Code
import java.util.*;
public class transpose
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Size");
int n=sc.nextInt();
int a[][]=new int[n][n];
int i,j,k;

## Page 130 of 148

System.out.println("Enter Array");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();;
}
}
System.out.println("Original Array");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
int temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
}
System.out.println("New Array");
for(i=0;i<n;i++)

## Page 131 of 148

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

}
}

## Page 132 of 148

Question 43.
Write a program to input a 2-D array and print its mirror matrix.
Algorithm :
STEP 1 - START.
STEP 2 - INPUT ELEMENTS IN ARRAY
STEP 3 - PRINT ORIGINAL ARRAY
STEP 4 - START LOOP FROM i = 0 , TO i<m , REPEAT STEP 5
STEP 5 - START LOOP FROM j = m-1 ,TO j>=0 , REPEAT STEP 6
STEP 6 - PRINT a[I][J]+" "
STEP 7 - IF SIZE ( m ) IS OUT OF RANGE ( m>20 )
PRINT "SIZE OUT OF RANGE"
STEP 8 - END

## Page 133 of 148

Programming Code :
import java.util.*;
class MatrixImage
{
public static void main(String arg[])
{
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
System.out.println("Enter size of an Array from 2 to 20 ");
m=ob.nextInt( );
if(m>1 && m<=20)
{
a=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)

## Page 134 of 148

{

a[i][j]=ob.nextInt( );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
System.out.println("Image Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=m-1;j>=0;j--)
System.out.print(a[i][j]+" ");
System.out.println( );
}
}
else System.out.println("SIZE OUT OFF RANGE");
}
}

## Page 136 of 148

Question 44.

Question 45.
Write a program to input a 2-D array and find whether it is a Lower Triangular Matrix or not.
Lower Triangular matrix is a square matrix in which all the entries above the main diagonal () are zero. The
entries below or on the main diagonal themselves may or may not be zero.
Algorithm :
Programming Code :

import java.util.*;
class LowerTriangularMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
int A[][]=new int[m][m];
for(int i=0;i<m;i++)

## Page 137 of 148

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

A[i][j]=sc.nextInt();
}
}
System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int p=0;
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
if(A[i][j]!=0)
{
p=1;
break;
}
}
}

## Page 138 of 148

if(p==0)
System.out.println("The matrix is Lower Triangular");
else
System.out.println("The matrix is not Lower Triangular");
}
}

## Page 139 of 148

Question 46.

Question 47.
Write a program to input a 2-D array and check whether it is a Upper Triangular Matrix or not.
An Upper Triangular matrix is a square matrix in which all the entries below the main diagonal () are zero. The
entries above or on the main diagonal themselves may or may not be zero.
Algorithm :
Programming Code :
import java.util.*;
class UpperTriangularMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);

## Page 140 of 148

System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
int A[][]=new int[m][m];
System.out.println("Enter Array");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{

A[i][j]=sc.nextInt();
}
}
System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int p=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<i;j++)
{
if(A[i][j]!=0)
p=1;

## Page 141 of 148

break;
}
}
}
if(p==0)
System.out.println("The matrix is Upper Triangular");
else
System.out.println("The matrix is not Upper Triangular");
}
}

## Page 142 of 148

Question 48.
Write a program to store numbers in a m x n matrix . Find the sum of elements in each row and
each column . Display the sum of each row and column corresponding to the rows and column.
Algorithm :
Programming Code :
import java.util.*;
public class sum
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n,m,i,j,s;
System.out.println("Enter the rows and column of the matrix");
m=sc.nextInt();
n=sc.nextInt();
int a[][]=new int[m+1][n+1];
System.out.println("Enter the Array");

## Page 143 of 148

for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Array ");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(i=0;i<m;i++)
{
s=0;
for(j=0;j<n;j++)
s+=a[i][j];
a[m][i]=s;
}
for(i=0;i<m;i++)
{
s=0;
for(j=0;j<n;j++)
s+=a[i][j];

## Page 144 of 148

a[i][n]=s;
}
System.out.println(" Final Matrix with the sum of each row and column is below :");
for(i=0;i<=m;i++)
{
for(j=0;j<=m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}

## Page 145 of 148

Question 49.

Question 50.
Write a program to store numbers in a 4x4 matrix. Arrange the numbers of each row in
ascending order and display the result.
Algorithm :

Programming Code :

import java.util.*;
public class Ascending_Row
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m[][]=new int[4][4];
int i,j,k,t;

## Page 146 of 148

System.out.println("Enter Array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
m[i][j]=sc.nextInt();
}
}
System.out.println("Original MAtrix ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3-j;k++)
{
if(m[i][k]>m[i][k+1])
{
t=m[i][k];
m[i][k]=m[i][k+1];
m[i][k+1]=t;

## Page 147 of 148

}
}
}
}
System.out.println("The Array after Sorting each row ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
}