Sum of two diagonal
import java.util.Scanner;
import java.util.*;
class diagnol{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int m[][]=new int[3][3],sum=0;
// int sum=0;
System.out.println("Enter the matrix ");
for(int i=0;i<m.length;i++){
for(int j=0;j<m[0].length;j++){
m[i][j]=s.nextInt();
}
}
//left diagnol sum
for(int i=0;i<m.length;i++){
for(int j=0;j<m[0].length;j++){
if(i==j){
sum=sum+m[i][j];
}
}
}
//right diagnol
for(int i=0;i<m.length;i++){
for(int j=0;j<m[0].length;j++){
if(i+j==m.length-1){
if(i!=j){
sum=sum+m[i][j];
}
}
}
}
System.out.println("Sum of both diagnol is :-");
System.out.print(sum);
}
}
Comments
Post a Comment