You are on page 1of 43

1. Program to accept and display a DDA.

import java.util.*;
class Prg_1
{ void main()
{ int a[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that every
4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
Output:
Enter the array elements which will be stored such that every 4 elements get stored in a
separate row.
5
7
4
3
6
7
8
8
8
6
5
2
3
5
4
9
The array is:
5743
6788
8652
3549

2. Program to accept a DDA and find the sum of


the elements in its rows and columns.

import java.util.*;
class Prg_1
{ void main()
{ int a[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that
every 4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int r[]={0,0,0,0};
int c[]={0,0,0,0};
int t=0;
for( i=0;i<4;i++)
{for( j=0;j<4;j++)
{ r[t]=r[t]+a[i][j];
c[t]=c[t]+a[j][i];}
t++;}
for( i=0;i<4;i++)
System.out.println("Sum of row "+(i+1)+" is "+r[i]);
for( j=0;j<4;j++)
System.out.println("Sum of column "+(j+1)+" is "+c[j]);
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get
stored in a separate row.
1
5
4
3

3
6
2
6
7
4
8
5
6
4
3
2
The array is:
1 5 4 3
3 6 2 6
7 4 8 5
6 4 3 2
Sum of row 1 is 13
Sum of row 2 is 17
Sum of row 3 is 24
Sum of row 4 is 15
Sum of column 1 is
Sum of column 2 is
Sum of column 3 is
Sum of column 4 is

17
19
17
16

3. Program to accept two 4x4 matrices and add


them.
import java.util.*;
class Prg_2
{ void main()
{int a[][]=new int [4][4];
int b[][]=new int [4][4];
int c[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array 1 elements which will be stored such
that every 4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}

}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("Enter the array 2 elements which will be stored such
that every 4 elements get stored in a separate row.");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
b[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(b[i][j] + " ");
}
System.out.println();
}
for( i=0;i<4;i++)
for( j=0;j<4;j++)
c[i][j]=a[i][j]+b[i][j];
System.out.println("Added matrix");
for( i=0;i<4;i++)
{ for( j=0;j<4;j++)
{ System.out.print( c[i][j]+" ");}
System.out.println();
}
}
}
Output:
Enter the array 1 elements which will be stored such that every 4 elements get
stored in a separate row.
5
2
3
3
3
3

3
3
6
4
6
4
7
8
5
4
The array is:
5 2 3 3
3 3 3 3
6 4 6 4
7 8 5 4
Enter the array 2 elements which will be stored such that every 4 elements get
stored in a separate row.
43
2
3
4
6
8
8
6
4
5
9
5
7
5
4
3
The array is:
43 2 3 4
6 8 8 6
4 5 9 5
7 5 4 3
Added matrix
48 4 6 7
9 11 11 9
10 9 15 9
14 3 9 7
15

4. Program to accept two 4x4 matrices and


subtract the second one from the first.

import java.util.*;

class Prg_2
{ void main()
{int a[][]=new int [4][4];
int b[][]=new int [4][4];
int c[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array 1 elements which will be stored such that
every 4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("Enter the array 2 elements which will be stored such that
every 4 elements get stored in a separate row.");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
b[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(b[i][j] + " ");
}
System.out.println();
}
for(i=0;i<4;i++)
for( j=0;j<4;j++)
c[i][j]=a[i][j]-b[i][j];
System.out.println("Subtracted matrix");
for(i=0;i<4;i++)
{ for( j=0;j<4;j++)

{ System.out.print( c[i][j]+" ");}


System.out.println();
}
}
}
Output:
Enter the array 1 elements which will be stored such that every 4 elements get
stored in a separate row.
8
6
5
4
3
5
7
8
9
0
56
4
2
4
6
5
The array is:
8 6 5 4
3 5 7 8
9 0 56 4
2 4 6 5
Enter the array 2 elements which will be stored such that every 4 elements get
stored in a separate row.
1
5
5
6
43
3
3
3
3
3
87
7
6
9
5
5

The array is:


1 5 5 6
43 3 3 3
3 3 87 7
6 9 5 5
Subtracted matrix
7 1 0 -2
-40 2 4 5
6 -3 -31 -3
-4 -5 1 0

5. Program to accept a 4x4 matrix and find the


product of odd numbers and sum of even
numbers.
import java.util.*;
class Prg_6
{ void main()
{int a[][]=new int[4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that every
4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int se=0,po=1;
for(i=0;i<4;i++)
for( j=0;j<4;j++)
if(a[i][j]%2==0)
se=se+a[i][j];
else
po=po*a[i][j];
System.out.println("Product of odd numbers is "+po);

System.out.println("Sum of even numbers is "+se);


}
}
Output:
Enter the array elements which will be stored such that every 4 elements get stored
in a separate row.
6
54
4
3
6
5
67
8
5
4
3
3
4
5
6
7
The array is:
6 54 4 3
6 5 67 8
5 4 3 3
4 5 6 7
Product of odd numbers is 1582875
Sum of even numbers is 92

6. Program to accept a 4x4 matrix and display


the prime elements.
import java.util.*;
class Prg_7
{ void main()
{int a[][]=new int[4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that every
4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)

{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
boolean x;
System.out.println("The prime elements:");
for( i=0;i<4;i++)
for( j=0;j<4;j++)
{ x=prime(a[i][j]);
if(x)
System.out.println(a[i][j]);
}
}
static boolean prime(int n)
{ int cn=0;
for(int i=1;i<=n;i++)
if(n%i==0)
cn++;
if(cn==2)
return true;
else
return false;
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get stored
in a separate row.
4
7
5
5
4
23
1
2
4
6
8
0

9
8
7
7
The array is:
4 7 5 5
4 23 1 2
4 6 8 0
9 8 7 7
The prime elements:
7
5
5
23
2
7
7

7. Program to accept a 4x4 matrix and sort the


elements.
import java.util.*;
class Prg_10
{ void main()
{int a[][]= {{3,1,4,2},{42,43,2,5},{2,4,5,1},{21,53,56,32}};
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that every
4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int b[]=new int[16];
int c=0;int t;
for(i=0;i<4;i++)
for( j=0;j<4;j++)

{b[c]=a[i][j];
c++;}
for( i=0;i<15;i++)
for(j=0;j<16-i-1;j++)
if(b[j]>b[j+1])
{t=b[j];
b[j]=b[j+1];
b[j+1]=t;}
System.out.println("After sorting");
for( i=0;i<16;i++)
System.out.println(b[i]);
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get stored
in a separate row.
6
5
5
4
4
4
4
7
8
2
3
41
2
3
6
8
The array is:
6 5 5 4
4 4 4 7
8 2 3 41
2 3 6 8
After sorting
2
2
3
3
4
4
4
4
5
5

6
6
7
8
8
41

8. Program to accept a 4x4 matrix and sort the


columns.
import java.util.*;
class Prg_13
{ void main()
{int a[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that
every 4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int b[]=new int [4];int c=0;
System.out.println("Array after sorting the columns");
for( int m=0;m<4;m++)
{for( int n=0;n<4;n++)
{ b[c]=a[n][m];c++;
if(n==4-1)
{int t;
for( i=0;i<4-1;i++)
for( j=0;j<4-i-1;j++)
if(b[j]>b[j+1])
{t=b[j];
b[j]=b[j+1];
b[j+1]=t;}
c=0;
for(int x=0;x<4;x++)

{ for(int y=0;y<4;y++)
{ if(x==m)
{a[y][x]=b[c];c++;}
}
}
c=0;
}
}
}
for( i=0;i<4;i++)
{ for( j=0;j<4;j++)
{ System.out.print(a[i][j]+" ");}
System.out.println();
}
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get
stored in a separate row.
1
2
4
5
4
8
7
6
5
4
3
1
2
3
5
6
The array is:
1 2 4 5
4 8 7 6
5 4 3 1
2 3 5 6
Array after sorting the columns
1231
2345
4456
5 876

9. Program to accept a 4x4 matrix and find the


highest element in each row.
import java.util.*;
class highest
{ void main()
{int a[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that every
4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int h=0;
for( i=0;i<4;i++)
{ for( j=0;j<4;j++)
{if(a[i][j]>h)
h=a[i][j];}
System.out.println("The highest in row "+(i+1)+" is "+h);
h=0;
}
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get stored
in a separate row.
5
4
7
3
5
8
9
6
4
2

1
3
5
6
7
8
The array is:
5 4 7 3
5 8 9 6
4 2 1 3
5 6 7 8
The highest
The highest
The highest
The highest

in
in
in
in

row
row
row
row

1
2
3
4

is
is
is
is

7
9
4
8

10. Program to accept a 4x4 matrix and find


the highest element in each column.
import java.util.*;
class highest2
{ void main()
{int a[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that every
4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int h=0;
for( i=0;i<4;i++)
{ for( j=0;j<4;j++)
{if(a[j][i]>h)

h=a[j][i];}
System.out.println("The highest in "+(i+1)+" column is "+h);
h=0;
}
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get stored
in a separate row.
7
6
6
6
5
4
3
5
8
7
6
5
3
2
4
6
The array is:
7 6 6 6
5 4 3 5
8 7 6 5
3 2 4 6
The highest
The highest
The highest
The highest

in
in
in
in

1
2
3
4

column
column
column
column

is
is
is
is

8
7
6
6

11. Program to accept a 4x4 matrix and


reverse the row order.
import java.util.*;
class Prg_16
{ void main()
{int a[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that every
4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)

{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int b[]=new int [4];int c=0;
for( i=0;i<4/2;i++)
{ for( j=0;j<4;j++)
{ b[c]=a[i][j];
int x=4-i-1;
//for(int x=4-i+1;x>=0;x++)
a[i][j]=a[x][j];
a[x][j]=b[c];
c++;
}
c=0;
}
System.out.println("The reversed array is:");
for( i=0;i<4;i++)
{ for( j=0;j<4;j++)
{ System.out.print(a[i][j]+" ");}
System.out.println();
}
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get
stored in a separate row.
1
6
3
7
8
6
6
6
5
4
3
2
3
4
5

6
The array is:
1 6 3 7
8 6 6 6
5 4 3 2
3 4 5 6
The reversed array is:
3456
5432
8666
1 637

12. Program to accept a 4x4 matrix and


interchange the first and last rows.
import java.io.*;
class Prg_17
{ void main()throws IOException
{ BufferedReader ob= new BufferedReader(new
InputStreamReader(System.in));
int a[][]=new int [4][4];int b[]=new int[4];int c=0;
System.out.println("Enter the elements");
for(int i=0;i<4;i++)
{ for(int j=0;j<4;j++)
a[i][j]=Integer.parseInt(ob.readLine());
}
System.out.println("The array is: \n");
for(int i=0;i<4;i++)
{
for(int j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
for(int i=0;i<4;i++)
{ for(int j=0;j<4;j++)
if(i==0)
{b[c]=a[i][j];
a[i][j]=a[3][j];
a[3][j]=b[c];
c++;}
}
System.out.println("The array after interchanging:");
for(int i=0;i<4;i++)
{ for(int j=0;j<4;j++)

{ System.out.print(a[i][j]+" ");}System.out.println();}}}
Output:
Enter the elements
6
3
4
5
7
8
9
3
2
1
2
3
4
5
7
7
The array is:
6 3 4 5
7 8 9 3
2 1 2 3
4 5 7 7
The array after interchanging:
4577
7893
2123
6 345

13. Program to accept a 5x5 matrix and print


the elements without the right diagonal and
elements to its right.
import java.util.*;
class Prg_18
{ void main()
{ int a[][]=new int [5][5];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that
every 5 elements get stored in a separate row.");
int i, j;
for(i=0;i<5;i++)
{
for(j=0; j<5; j++)
{
a[i][j] = sc.nextInt();

}
}
System.out.println("The array is: \n");
for(i=0;i<5;i++)
{
for(j=0; j<5; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int c=0;
for( i=5-1;i>=0;i--)
{ for( j=5-1;j>=0;j--)
if(i==j)
{ for(int k=0;k<j;k++)
{System.out.print(a[c][k]+" ");}
System.out.println();c++;
}
}
}
}
Output:
Enter the array elements which will be stored such that every 5 elements get
stored in a separate row.
1
5
4
8
7
4
1
2
1
4
5
6
9
8
7
4
5
6
3
2
1
5
6
9
8

The array is:


1
4
5
4
1

5
1
6
5
5

4
2
9
6
6

8
1
8
3
9

7
4
7
2
8

1548
412
56
4

14. Program to print the last element of each


row except the first, of a DDA, in a pyramid
fashion.
class Prg_21
{ void main()
{ int a[][]={{13,22,14,23,21},{16,14,15,34,10},{71,11,10,40,12},
{41,13,11,56,19},{22,71,45,12,34}};
int c=0;
for(int i=5-1;i>=0;i--)
{ for(int j=5-1;j>=0;j--)
if(i==j)
{ for(int sp=0;sp<=j;sp++)
System.out.print(" ");
for(int k=j+1;k<5;k++)
{System.out.print(a[c][k]+" ");}
System.out.println();c++;
}
}
}
}
Output:
10
40 12
11 56 19
71 5 12 34

15. Program to accept a 4x4 matrix and print


the product of the elements of each row and
each column.
import java.util.*;
class Prg_23
{ void main()
{ int a[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that
every 4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
long pr=1,pc=1;
for( i=0;i<4;i++)
{ for( j=0;j<4;j++)
{pr=pr*a[i][j]; pc=pc*a[j][i];}
System.out.println("Product of column "+(i+1)+" :"+pc);
System.out.println("Product of row "+(i+1)+" :"+pr);
pr=pc=1;
}
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get
stored in a separate row.
5
4
4
3
3
5
7

8
9
7
6
5
4
4
3
2
The array is:
5 4 4 3
3 5 7 8
9 7 6 5
4 4 3 2
Product of
Product of
Product of
Product of
Product of
Product of
Product of
Product of

column 1 :540
row 1 :240
column 2 :560
row 2 :840
column 3 :504
row 3 :1890
column 4 :240
row 4 :96

16. Program to accept a matrix and print its


transpose.
import java.util.Scanner;
class TransposeAMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int matrix[][] = new int[m][n];
System.out.println("Enter the elements of matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
matrix[c][d] = in.nextInt();
System.out.println("The matrix is: \n");
for(c=0;c<m;c++)
{

for(d=0; d<n; d++)


{
System.out.print(matrix[c][d] + " ");
}
System.out.println();
}
int transpose[][] = new int[n][m];
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];
}
System.out.println("Transpose of entered matrix:-");
for ( c = 0 ; c < n ; c++ )
{
for ( d = 0 ; d < m ; d++ )
System.out.print(transpose[c][d]+"\t");
System.out.print("\n");
}
}
}
Output:
Enter the number of rows and columns of matrix
4
2
Enter the elements of matrix
1
2
3
4
5
6
7
8
The matrix is:
1 2
3 4
5 6
7 8
Transpose of entered matrix:1
3
5
7
2 4 6
8

17. Program to accept 2 matrices and multiply


them.
import java.util.Scanner;
class MatrixMultiplication
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first
matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();
System.out.println("Enter the number of rows and columns of second
matrix");
p = in.nextInt();
q = in.nextInt();
if ( n != p )
System.out.println("Matrices with entered orders can't be multiplied
with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("Enter the elements of second matrix");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
second[c][d] = in.nextInt();
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{

for ( k = 0 ; k < p ; k++ )


{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
System.out.println("Product of entered matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
}
}
}
}
Output:
Enter the number of rows and columns of first matrix
3
3
Enter the elements of first matrix
3
3
5
2
7
4
8
4
3
Enter the number of rows and columns of second matrix
3
3
Enter the elements of second matrix
7
5
4
3
2
7
5
4
8
Product of entered matrices:-

55
41
55
40
83 60 84

73
89

18. Program to accept a square matrix and find


the sum of its left diagonal elements.
import java.io.*;
public class Sum_Diagonal
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the size of 2D array :");
int i=Integer.parseInt(br.readLine());
int d[][]=new int[i][i];
int j,k;
int sum1=0,sum2=0;

BufferedReader br1=new BufferedReader(new


InputStreamReader(System.in));
System.out.println("Enter the values of 2D array of "+i+" * "+i+"
matrix ");

for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
{
d[j][k]=Integer.parseInt(br1.readLine());

}
System.out.println();
}

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

for(j=0;j<i;j++)
{
sum1=sum1+d[j][j];
}

k=i-1;
for(j=0;j<i;j++)
{
if(k>=0)
{
sum2=sum2+d[j][k];
k--;
}
}

System.out.println("Sum of left Diagonal elements are :"+sum1);


}
}
Output:
Enter the size of 2D array :
4
Enter the values of 2D array of 4 * 4 matrix
5
4
3
2
4
7
6
5
4
3
8
5
2
3
5
4
5432
4765
4385
2354
Sum of left Diagonal elements are :24

19. Program to accept a square matrix and find


the sum of its right diagonal elements.
import java.io.*;
public class Sum_Diagonal
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the size of 2D array :");
int i=Integer.parseInt(br.readLine());

int d[][]=new int[i][i];


int j,k;
int sum1=0,sum2=0;

BufferedReader br1=new BufferedReader(new


InputStreamReader(System.in));
System.out.println("Enter the values of 2D array of "+i+" * "+i+"
matrix ");

for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
{
d[j][k]=Integer.parseInt(br1.readLine());
}
System.out.println();
}

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

for(j=0;j<i;j++)

{
sum1=sum1+d[j][j];
}

k=i-1;
for(j=0;j<i;j++)
{
if(k>=0)
{
sum2=sum2+d[j][k];
k--;
}
}
System.out.println("Sum of right diagonal elements are :"+sum2);
}
}

20. Program to accept a square matrix and find


the sum of its diagonal elements for both
diagonals.
import java.io.*;
public class Sum_Diagonal
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

System.out.println("Enter the size of 2D array :");


int i=Integer.parseInt(br.readLine());
int d[][]=new int[i][i];
int j,k;
int sum1=0,sum2=0;

BufferedReader br1=new BufferedReader(new


InputStreamReader(System.in));
System.out.println("Enter the values of 2D array of "+i+" * "+i+"
matrix ");

for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
{
d[j][k]=Integer.parseInt(br1.readLine());
}
System.out.println();
}

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

for(j=0;j<i;j++)
{
sum1=sum1+d[j][j];
}

k=i-1;
for(j=0;j<i;j++)
{
if(k>=0)
{
sum2=sum2+d[j][k];
k--;
}
}
System.out.println("Sum of Diagonal elements are :"+sum1+" and
"+sum2);
}
}
Output:
Enter the size of 2D array :
5
Enter the values of 2D array of 5 * 5 matrix
1
3
6
9
8
7
4
5
2
1
4

6
3
2
5
6
9
8
7
4
1
2
3
6
5
13698
74521
46325
69874
12365
Sum of Diagonal elements are :20 and 23

21. Program to accept a square matrix and find


the sum of the sums of both its diagonals.
import java.io.*;
public class Sum_Diagonal
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the size of 2D array :");
int i=Integer.parseInt(br.readLine());
int d[][]=new int[i][i];
int j,k;
int sum1=0,sum2=0;

BufferedReader br1=new BufferedReader(new


InputStreamReader(System.in));
System.out.println("Enter the values of 2D array of "+i+" * "+i+"
matrix ");

for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
{
d[j][k]=Integer.parseInt(br1.readLine());
}
System.out.println();
}

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

for(j=0;j<i;j++)
{
sum1=sum1+d[j][j];
}

k=i-1;
for(j=0;j<i;j++)
{
if(k>=0)
{
sum2=sum2+d[j][k];
k--;
}
}
System.out.println("Sum of the sum of both its diagonal
elements :"+(sum1+sum2));
}
}
Output:
Enter the size of 2D array :
3
Enter the values of 2D array of 3 * 3 matrix
5
4
5
6
9
8
7
2
1
545
698
721
Sum of the sum of both its diagonal elements :36

22. Program to accept a square matrix and find


the difference between the sums of its diagonal
elements.

import java.io.*;
import java.lang.*;

public class Sum_Diagonal


{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the size of 2D array :");
int i=Integer.parseInt(br.readLine());
int d[][]=new int[i][i];
int j,k;
int sum1=0,sum2=0;

BufferedReader br1=new BufferedReader(new


InputStreamReader(System.in));
System.out.println("Enter the values of 2D array of "+i+" * "+i+"
matrix ");

for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
{
d[j][k]=Integer.parseInt(br1.readLine());
}

System.out.println();
}

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

for(j=0;j<i;j++)
{
sum1=sum1+d[j][j];
}

k=i-1;
for(j=0;j<i;j++)
{
if(k>=0)
{
sum2=sum2+d[j][k];
k--;
}
}
System.out.println("Difference between the sums of both its
diagonal elements :"+(Math.abs(sum1-sum2)));

}
}
Output:
Enter the size of 2D array :
3
Enter the values of 2D array of 3 * 3 matrix
1
2
3
6
5
4
7
8
9
123
654
789
Difference between the sums of both its diagonal elements :0

23.

Program to bubble sort an SDA.

import java.util.*;
class bubsort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[] = new int[5];
int i,k,t,sn,ss;
System.out.println("Enter 5 numbers :");
for(i=0;i<5;i++)
{
a[i]= sc.nextInt();
}
for(i=0;i<4;i++)
{
for(k=0;k<4;k++)
{
if(a[k] > a[k+1])
{
t=a[k];
a[k]=a[k+1];
a[k+1]=t;
}}}
System.out.println("The sorted array is :");

for(i=0;i<5;i++)54
{
System.out.println(a[i]);
}
}}
Output:
Enter 5 numbers :
51
23
56
89
78
The sorted array is :
23
51
56
78
89

24.

Program to selection sort an SDA.

import java.io.*;
public class SelectionSort
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int a[] = new int[5];
int i,k,t,min,minpos;
// To accept the number you are looking for
System.out.println("Enter 5 numbers :");
for(i=0;i<5;i++)
{
System.out.println("Enter the next number.");
a[i]= Integer.parseInt(br.readLine());
}
//To arrange number in ascending order
for(i=0;i<=3;i++)
{
min=a[i];
minpos=i;
for(k=i+1;k<=4;k++)
{
if(a[k] < min)
{
min=a[k];
minpos=k;
}
}

t=a[i];a[i]=a[minpos];a[minpos]=t;
}
//To print the array
System.out.println("The sorted array is :");
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
}}
Output:
Enter 5 numbers :
Enter the next number.
6
Enter the next number.
5
Enter the next number.
4
Enter the next number.
1
Enter the next number.
3
The sorted array is :
1
3
4
5
6

25. Program accept an SDA and binary search


an element.
import java.util.*;
class BinarySort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int A[] = new int[5];
int i,j,ub=A.length-1; int mid=A.length/2; int lb=0;
System.out.println("Enter 5 numbers :");
for(i=0;i<5;i++)
{
A[i]= sc.nextInt();
}
System.out.println("Enter the search term :");
int search = sc.nextInt();
boolean flag = false;
do
{
mid = (ub+lb)/2;

if(search==A[mid])
{
System.out.println("Found");
flag = true;
break;
}
else if(search>A[mid])
{
lb = mid+1;
}
else{
ub = mid-1;
}
}while(ub!=lb);
if(flag == false)
{
System.out.println("Not Found");
}
}
}
Output:
Enter 5 numbers :
78
12
32
56
48
Enter the search term :
48
Found

You might also like