-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix_diagonal.java
61 lines (39 loc) · 1.2 KB
/
Matrix_diagonal.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* Finding the difference of diagonals in a matrix.........
........................................
..........................................*/
import java.util.Scanner;
public class Matrix_diagonal
{
public static void main(String[] args)
{
int m, n;
Scanner s = new Scanner(System.in);
System.out.print("Enter matrix size:");
m= n = s.nextInt(); //size of matrix..
int a[][] = new int[m][n];
System.out.println("Enter all the elements of first matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = s.nextInt();
}
}
int d1=0 , d2= 0;
for(int i=0; i<n;i++) {
d1+=a[i][i]; /* diagonal first , 0,0, 1,1, 2,2 ......*/
d2+=a[i][n-i-1]; /* diagonal first , 3,0, 2,1, 1,0 ......*/
}
System.out.println(d1);
System.out.println(d2);
int diff = d1-d2;
// for(int i=0;i<n;i++) //printing the array.
// {
// for (int j=0;j<m;j++)
// {
// System.out.print(a[i][j]+" ");
// }
// System.out.println("\n");
// }
}
}