A simple program to show how to solve the sum of diagonal elements in a matrix using Java programming language.
I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.
My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.Program Listingimport java.util.*; class matrix { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int i,j,row,col,sum=0; System.out.println("Enter the number of rows:"); row = sc.nextInt(); System.out.println("Enter the number of columns:"); col = sc.nextInt(); int[][] mat = new int[row][col]; System.out.println("Enter the elements of the matrix") ; for(i=0;i<row;i++) { for(j=0;j<col;j++) { mat[i][j] = sc.nextInt(); } } System.out.println("The elements of the matrix") ; for(i=0;i<row;i++) { for(j=0;j<col;j++) { System.out.print(mat[i][j]+"\t"); } System.out.println(""); } for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(i+j==2) //this condition checks for diagonal { sum = sum + mat[i][j]; } } } System.out.printf("SUM of DIAGONAL elements of the matrix = "+sum) ; } }
No comments:
Post a Comment