java program to find trace of a matrix

Solutions on MaxInterview for java program to find trace of a matrix by the best coders in the world

showing results for - "java program to find trace of a matrix"
Sean
18 Sep 2017
1// Java program to find trace of a matrix
2import java.util.Scanner;
3public class TraceMatrixDemo 
4{
5   public static void main(String[] args) 
6   {
7      int[][] arrInput = new int[10][10];
8      int a, b;
9      double total = 0;
10      System.out.println("Please enter total rows and columns: ");
11      Scanner sc = new Scanner(System.in);
12      int row = sc.nextInt();
13      int column = sc.nextInt();
14      System.out.println("Please enter matrix: ");
15      for(a = 0; a < row; a++)
16      {
17         for(b = 0; b < column; b++) 
18         {
19            arrInput[a][b] = sc.nextInt();
20            System.out.print(" ");
21         }
22      }
23      System.out.println("Entered matrix is: ");
24      for(a = 0; a < row; a++)
25      {
26         for(b = 0; b < column; b++)
27         {
28            System.out.println(arrInput[a][b] + " ");
29         }
30         System.out.println(" ");
31      }
32      System.out.println("Trace of a matrix: ");
33      for(a = 0; a < row; a++)
34      {  
35         for(b = 0; b < column; b++)
36         {
37            if(a == b)
38            {
39               total = total + (arrInput[a][b]);
40            }
41         }
42      }
43      System.out.println(total);
44      sc.close();
45   }
46}