program aritmatika matriks dengan java


program penjumlahan, pengurangan, dan perkalian matriks dengan java

import java.util.Scanner;
class matrix {
public static void main(String[] args) {
}
 int i,j,x,y,pil;
 int [][] ma = new int [3][3];
 int [][] mb = new int [3][3];
  
        System.out.println("Matriks A");
 for (i=0;i<=2;i++) {
 j=i;
 j++;
      for (x=0;x<=2;x++) {
      y=x;
      y++;
      System.out.print("Masukkan bilangan pada baris "+j+" kolom "+y+" = ");
      Scanner a = new Scanner(System.in);
      ma[i][x] = a.nextInt();
      }
 }
 System.out.println();
        System.out.println("Matriks B");
 for (i=0;i<=2;i++) {
 j=i;
 j++;
      for (x=0;x<=2;x++) {
      y=x;
      y++;
      System.out.print("Masukkan bilangan pada baris "+j+" kolom "+y+" = ");
      Scanner a = new Scanner(System.in);
      mb[i][x] = a.nextInt();
      }
 }
 System.out.println("Matriks A");
 System.out.println("--                   --");        
 System.out.printf("|%5d %5d %5d    |", ma[0][0], ma[0][1], ma[0][2]); System.out.println();
 System.out.printf("|%5d %5d %5d    |", ma[1][0], ma[1][1], ma[1][2]); System.out.println();
 System.out.printf("|%5d %5d %5d    |", ma[2][0], ma[2][1], ma[2][2]); System.out.println();
 System.out.printf("--                   --");
 System.out.println();

 System.out.println("Matriks B");
 System.out.println("--                   --");        
 System.out.printf("|%5d %5d %5d    |", mb[0][0], mb[0][1], mb[0][2]); System.out.println();
 System.out.printf("|%5d %5d %5d    |", mb[1][0], mb[1][1], mb[1][2]); System.out.println();
 System.out.printf("|%5d %5d %5d    |", mb[2][0], mb[2][1], mb[2][2]); System.out.println();
 System.out.printf("--                   --");
 System.out.println();

 System.out.println("Menu operasi");
 System.out.println("1. Penjumlahan Matrik");
 System.out.println("2. Pengurangan Matrik");
 System.out.println("3. Perkalian Matrik");
 System.out.println();
 System.out.print("Pilih no menu operasi = ");
 Scanner no = new Scanner(System.in);
 pil = no.nextInt();
 System.out.println();
 switch(pil) {
 case 1 : jumlah(ma,mb);
     break;
 case 2 : kurang(ma,mb);
    break;
 case 3 : kali(ma,mb);
          break;
 default : System.out.println("Tidak ada dalam pilihan");
 }
}

 public static void jumlah(int [][] a, int [][] b) {
 int m [][] = new int [3][3];
 int i,x;
 for (i=0;i<=2;i++) {
 for (x=0;x<=2;x++) {
      m[i][x] =  a[i][x] + b[i][x];
     
 }
 }
 System.out.println("Hasil penjumlahan matrik");
 System.out.println("--                   --");        
 System.out.printf("|%5d %5d %5d    |", m[0][0], m[0][1], m[0][2]); System.out.println();
 System.out.printf("|%5d %5d %5d    |", m[1][0], m[1][1], m[1][2]); System.out.println();
 System.out.printf("|%5d %5d %5d    |", m[2][0], m[2][1], m[2][2]); System.out.println();
 System.out.printf("--                   --");
 System.out.println();                                         
 }



 public static void kurang(int [][] a, int [][] b) {
 int m [][] = new int [3][3];
 int i,x;
 for (i=0;i<=2;i++) {
 for (x=0;x<=2;x++) {
      m[i][x] =  a[i][x] - b[i][x];
 }
 }
 System.out.println("Hasil pengurangan matrik");
 System.out.println("--                   --");        
 System.out.printf("|%5d %5d %5d    |", m[0][0], m[0][1], m[0][2]); System.out.println();
 System.out.printf("|%5d %5d %5d    |", m[1][0], m[1][1], m[1][2]); System.out.println();
 System.out.printf("|%5d %5d %5d    |", m[2][0], m[2][1], m[2][2]); System.out.println();
 System.out.printf("--                   --");
 System.out.println();
 }



 public static void kali(int [][] a, int [][] b) {
 int m [][] = new int [3][3];
 int i,x,y;
 for (i=0;i<=2;i++) {
 for (x=0;x<=2;x++) {
      for(y=0;y<=2;y++) {
       m[i][x] =  m[i][x] + (a[i][y] * b[y][x]);
  
 }
 }
 }
 System.out.println ("Hasil perkalian matrik");
 System.out.println("--                   --");        
 System.out.printf("|%5d %5d %5d    |", m[0][0], m[0][1], m[0][2]); System.out.println();
 System.out.printf("|%5d %5d %5d    |", m[1][0], m[1][1], m[1][2]); System.out.println();
 System.out.printf("|%5d %5d %5d    |", m[2][0], m[2][1], m[2][2]); System.out.println();
 System.out.printf("--                   --");
 System.out.println();
 }

}



Post a Comment

0 Comments