HOW TO SUM 3*3 MATRIX OF AN 2D ARRAY 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;
int sum=0;
System.out.println("Enter the matrix ");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
n[i][j]=sc.nextInt();
}
}
System.out.println("Your matrix ");
for(i=0;i<3;i++){
sum=0;
for(j=0;j<3;j++){
sum=sum+n[i][j];
System.out.print(" " + n[i][j]);
}
System.out.println("=" + sum);
}
}
}
0 Comments