51
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

Programs Project

Embed Size (px)

DESCRIPTION

Introductory level Java Programs

Citation preview

Page 1: Programs Project

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.5743678886523549The array is:

574367888652

Page 2: Programs Project

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:

Page 3: Programs Project

Enter the array elements which will be stored such that every 4 elements get stored in a separate row.1543362674856432The array is:

1 5 4 3 3 6 2 6 7 4 8 5 6 4 3 2 Sum of row 1 is 13Sum of row 2 is 17Sum of row 3 is 24Sum of row 4 is 15Sum of column 1 is 17Sum of column 2 is 19Sum of column 3 is 17Sum of column 4 is 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.");

Page 4: Programs Project

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(); } }}

Page 5: Programs Project

Output:Enter the array 1 elements which will be stored such that every 4 elements get stored in a separate row.5233333364647854The 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.43234688645957543The array is:

43 2 3 4 6 8 8 6 4 5 9 5 7 5 4 3

Page 6: Programs Project

Added matrix48 4 6 7 9 11 11 9 10 9 15 9

14 3 9 7

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++)

Page 7: Programs Project

{ 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.86543578905642465The 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. 155

Page 8: Programs Project

643333338776955The array is:

1 5 5 6 43 3 3 3 3 3 87 7 6 9 5 5 Subtracted matrix7 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++) {

Page 9: Programs Project

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.654436567854

334567The array is:

6 54 4 3 6 5 67 8 5 4 3 3 4 5 6 7 Product of odd numbers is 1582875Sum of even numbers is 92

Page 10: Programs Project

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; }

Page 11: Programs Project

}

Output:Enter the array elements which will be stored such that every 4 elements get stored in a separate row.47554231246809877The array is:

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

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++) {

Page 12: Programs Project

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.655444478234123

Page 13: Programs Project

68The array is:

6 5 5 4 4 4 4 7 8 2 3 41 2 3 6 8 After sorting22334444556678841

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++) {

Page 14: Programs Project

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.12454876

Page 15: Programs Project

54312356The array is:

1 2 4 5 4 8 7 6 5 4 3 1 2 3 5 6 Array after sorting the columns1 2 3 1 2 3 4 5 4 4 5 6 5 8 7 6

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();

Page 16: Programs Project

} 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.5473589642135678The array is:

5 4 7 3 5 8 9 6 4 2 1 3 5 6 7 8 The highest in row 1 is 7The highest in row 2 is 9The highest in row 3 is 4The highest in row 4 is 8

10. Program to accept a 4x4 matrix and find the highest element in each column.

import java.util.*;class highest2

Page 17: Programs Project

{ 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.76665435876532

Page 18: Programs Project

46The array is:

7 6 6 6 5 4 3 5 8 7 6 5 3 2 4 6 The highest in 1 column is 8The highest in 2 column is 7The highest in 3 column is 6The highest in 4 column is 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];

Page 19: Programs Project

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.1637866654323456The array is:

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

12. Program to accept a 4x4 matrix and interchange the first and last rows.import java.io.*;

Page 20: Programs Project

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 elements6345789321234

Page 21: Programs Project

577The array is:

6 3 4 5 7 8 9 3 2 1 2 3 4 5 7 7 The array after interchanging:4 5 7 7 7 8 9 3 2 1 2 3 6 3 4 5

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--)

Page 22: Programs Project

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.1548741214569874563215698The array is:

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

1 5 4 8 4 1 2 5 6 4

Page 23: Programs Project

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();

Page 24: Programs Project

} } 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.5443357897654432The array is:

5 4 4 3 3 5 7 8 9 7 6 5 4 4 3 2 Product of column 1 :540

Page 25: Programs Project

Product of row 1 :240Product of column 2 :560Product of row 2 :840Product of column 3 :504Product of row 3 :1890Product of column 4 :240Product of 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]; }

Page 26: Programs Project

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 matrix42Enter the elements of matrix12345678The matrix is:

1 2 3 4 5 6 7 8 Transpose of entered matrix:-1 3 5 72 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");

Page 27: Programs Project

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");

Page 28: Programs Project

System.out.print("\n"); } } }}Output:Enter the number of rows and columns of first matrix33Enter the elements of first matrix335274843Enter the number of rows and columns of second matrix33Enter the elements of second matrix754327548Product of entered matrices:-55 41 7355 40 8983 60 84

18. Program to accept a square matrix and find the sum of its left diagonal elements.

import java.io.*;public class Sum_Diagonal

{

Page 29: Programs Project

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++)

Page 30: Programs Project

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 :4Enter the values of 2D array of 4 * 4 matrix 5432

Page 31: Programs Project

476543852354

5 4 3 2 4 7 6 5 4 3 8 5 2 3 5 4 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 ");

Page 32: Programs Project

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++)

Page 33: Programs Project

{

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;

Page 34: Programs Project

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];

}

Page 35: Programs Project

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 :5Enter the values of 2D array of 5 * 5 matrix 1369874521463256987412

Page 36: Programs Project

365

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 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++)

Page 37: Programs Project

{

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)

Page 38: Programs Project

{

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 :3Enter the values of 2D array of 3 * 3 matrix 545

698

721

5 4 5 6 9 8 7 2 1

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

Page 39: Programs Project

{

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++)

{

Page 40: Programs Project

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 :3Enter the values of 2D array of 3 * 3 matrix 1

Page 41: Programs Project

23

654

789

1 2 3 6 5 4 7 8 9 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:

Page 42: Programs Project

Enter 5 numbers :5123568978The sorted array is :2351567889

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 forSystem.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++)

Page 43: Programs Project

{ System.out.println(a[i]);

} }}Output:Enter 5 numbers :Enter the next number.6Enter the next number.5Enter the next number.4Enter the next number.1Enter the next number.3The sorted array is :13456

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]){

Page 44: Programs Project

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 :7812325648Enter the search term :48Found