forked from slayk/Hacktoberfest21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixDiagSum.java
More file actions
39 lines (36 loc) · 987 Bytes
/
MatrixDiagSum.java
File metadata and controls
39 lines (36 loc) · 987 Bytes
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
34
35
36
37
38
39
Contributor Info :
// Name : Ankit Raj
// Git : https://github.com/Ankit-5087
import java.util.*;
class array2D_diag
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=4;
int num[][]=new int[n][n];
for (int r=0;r<n;r++)
{
System.out.println("Enter integers for Row No. "+r);
for (int c=0;c<n;c++)
{
num[r][c]=sc.nextInt();
}
}
int diag1=0,diag2=0;
for (int r=0;r<n;r++)
{
for (int c=0;c<n;c++)
{
System.out.print(num[r][c]+"\t");
if (r==c)
diag1=diag1+num[r][c];
else if (r+c==n-1)
diag2=diag2+num[r][c];
}
System.out.println();
}
System.out.println("Sum of 1st diagonals = "+diag1);
System.out.println("Sum of 2nd diagonals = "+diag2);
}
}