public class MatrikJumlah{
public static void main(String[]args){
int[][]matrik1={{1,2,3,4},{5,6,7,8}};
int[][]matrik2={{5,6,7,8},{1,2,3,4}};
int[][]jumlah=new int[matrik1.length][matrik1[0].length];
System.out.println("Data Matrik 1 :");
for(int i=0;i<matrik1.length;i++){
for(int j=0;j<matrik1[0].length;j++){
System.out.print(matrik1[i][j]+" ");
}
System.out.println();
}
System.out.println("Data Matrik 2 :");
for(int i=0;i<matrik2.length;i++){
for(int j=0;j<matrik2[0].length;j++){
System.out.print(matrik2[i][j]+" ");
}
System.out.println();
}
System.out.println("Hasil Penjumlahan : ");
for(int i=0;i<jumlah.length;i++){
for(int j=0;j<jumlah[0].length;j++){
jumlah[i][j]=matrik1[i][j]+matrik2[i][j];
System.out.print(jumlah[i][j]+" ");
}
System.out.println();
}
}
output :
1 2 3 4
5 6 7 8
Data Matrik 2 :
5 6 7 8
1 2 3 4
Hasil Penjumlahan :
6 8 10 12
6 8 10 12
Tags:
Java