-
Notifications
You must be signed in to change notification settings - Fork 5
/
SubSumOfDiag2Matrix.java
46 lines (43 loc) · 1.11 KB
/
SubSumOfDiag2Matrix.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
/* Sum of substraction of two diagonals of a matrix
Enter number of row/col :3
1 2 3
4 5 6
9 8 9
The substraction of the sums of each diagonals : 2
* |(1+5+9)-(3+5+9)| = 2
*/
import java.util.*;
public class SubSumOfDiag2Matrix
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of row/col :");
int n = sc.nextInt();
int[][] matrix = new int[n][n];
int sum1=0, sum2=0,sub;
for (int r = 0; r < matrix.length; r++)
{
for (int c = 0; c < matrix.length; c++)
{
matrix[r][c]=sc.nextInt();
if(r==c)
{
sum1 = sum1+matrix[r][c];
}
if(r==(matrix.length-1-c))
{
sum2 = sum2+matrix[r][c];
}
}
}
if(sum1>sum2)
{
sub = sum1-sum2;
}
else
{
sub = sum2-sum1;
}
System.out.println("The substraction of the sums of each diagonals : "+sub);
}
}