HOW TO DIAGONAL SUM OF 3*3 MATRIX IN JAVA?
INPUT:-
import java.util.Scanner;
public class example{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n[][]= new int[3][3];
int i,j,sum=0;
System.out.println("Enter your element : ");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
n[i][j]=sc.nextInt();
}
}
System.out.println("Your elements : ");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
System.out.print(" " + n[i][j]);
}
System.out.println();
}
System.out.println("Your sum Elements ");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
if(n[i]==n[j]){
sum=sum+n[i][j];
System.out.println(" " + n[i][j]);
}
}
}
System.out.println("=" + sum);
}
}
0 Comments