-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfDiagonal.java
More file actions
40 lines (34 loc) · 1.1 KB
/
SumOfDiagonal.java
File metadata and controls
40 lines (34 loc) · 1.1 KB
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
import java.util.Scanner;
public class SumOfDiagonal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows of first matrix: ");
int rows = sc.nextInt();
System.out.print("\nEnter the number of columns of first matrix: ");
int columns = sc.nextInt();
int[][] arr =new int[rows][columns];
System.out.print("\nEnter the elements of the first array:\n ");
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
arr[i][j]=sc.nextInt();
}
}
int i=0;
int mainDiagonalSum=0;
while(i<rows && i < columns){
mainDiagonalSum += arr[i][i];
i++;
}
System.out.println("Sum of Main Diagonal: "+mainDiagonalSum);
int j=columns-1;
i=0;
int subDiagonalSum=0;
while(i<rows && j>=0){
subDiagonalSum += arr[i][j];
i++;
j--;
}
System.out.println("Sum of Sub Diagonal: "+subDiagonalSum);
sc.close();
}
}