-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.java
33 lines (33 loc) · 823 Bytes
/
trace.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Scanner;
class trace
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the row size");
int r=sc.nextInt();
System.out.println("Enter the col size");
int c=sc.nextInt();
System.out.println("Enter the elements in matrix");
int[][] a=new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
a[i][j]=sc.nextInt();
}
}
int sum=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(i==j)
{
sum+=a[i][j];
}
}
}
System.out.println("the trace of the matrix is:"+sum);
}
}