Posted in Program Java

Program Bahasa Java Penjumlahan dan Perkalian Matriks Ordo 2*2

Selamat siang bloggers, di awal bulan maret ini aku bakal share program penjumlahan dan perkalian matriks ordo 2*2 dalam bahasa java. Langsung aja ya Cekidot :p

Penjumlahan Matriks

 
package penjumlahanmatriks;
import java.util.Scanner;
/**
 *
 * @author Rifa'atul Mahmudah
 */
public class PenjumlahanMatriks {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input= new Scanner (System.in);
        int A[][]=new int[2][2];
        int B[][]=new int[2][2];
        int C[][]=new int[2][2];
        
        System.out.println("Masukkan Matriks A");
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                System.out.print("[" +(i+1)+ "][" +(j+1)+ "]:");
                A[i][j]=input.nextInt();
            }
        }
        System.out.println("Masukkan Matriks B");
        for(int k=0;k<2;k++){
            for(int l=0;l<2;l++){
                System.out.print("[" +(k+1)+ "][" +(l+1)+ "]:");
                B[k][l]=input.nextInt();
            }
        }
        System.out.println("Matriks A");
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                System.out.print(+(A[i][j])+ " ");
            }
                System.out.println(" ");
        }
        System.out.println("Matriks B");
        for(int k=0;k<2;k++){
            for(int l=0;l<2;l++){
                System.out.print(+(B[k][l])+ " ");
            }
                System.out.println(" ");
        }
        for(int x=0;x<2;x++){
            for(int y=0;y<2;y++){
                C[x][y]=A[x][y]+B[x][y];
            }
        }
        System.out.println("Hasil penjumlahan Matriks A dan Matriks B");
        for(int x=0;x<2;x++){
            for(int y=0;y<2;y++){
                System.out.print(+(C[x][y])+" ");
            }
            System.out.println(" ");
        }
        
    }
    
}

outputnya gini nih

1

Perkalian Matriks
Selanjutnya perkalian Matriks, ga terlalu berbeda sama penjumlahan, cuma beda pas dioperasi perkaliannya aja, langsung aja nih

package perkalianmatriks;

import java.util.Scanner;
/**
 *
 * @author Rifa'atul Mahmudah
 */
public class PerkalianMatriks {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        int A[][]= new int[2][2];
        int B[][]= new int[2][2];
        int C[][]= new int[2][2];
        
        System.out.println("Masukkan matriks A");
        for (int i=0;i<2;i++){
            for (int j=0;j<2;j++){
                System.out.print("["+(i+1)+"]["+(j+1)+"]:");
                A[i][j]=input.nextInt();
            }
        }
        
        System.out.println("Masukkan matriks B");
        for(int k=0;k<2;k++){
            for(int l=0;l<2;l++){
                System.out.print("["+(k+1)+"]["+(l+1)+"]:");
                B[k][l]=input.nextInt();
            }
        }
        
        System.out.println("Matriks A");
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                System.out.print(A[i][j]+ " ");
            }
            System.out.println("");
        }
        
        System.out.println("Matriks B");
        for(int k=0; k<2;k++){
            for(int l=0; l<2;l++){
                System.out.print(B[k][l]+" ");
            }
            System.out.println("");
        }
        
        C[0][0]=A[0][0]*B[0][0]+A[0][1]*B[1][0];
        C[0][1]=A[0][0]*B[0][1]+A[0][1]*B[1][1];
        C[1][0]=A[1][0]*B[0][0]+A[1][1]*B[1][0];
        C[1][1]=A[1][0]*B[0][1]+A[1][1]*B[1][1];
        
        System.out.println("Hasil perkalian Matriks A dan B");
        for(int x=0;x<2;x++){
            for(int y=0;y<2;y++){
                System.out.print(C[x][y]+" ");
            }
            System.out.println(" ");
        }
    }
    
}

Outputnya

n2

Sekian dulu ya semoga bermanfaatπŸ™‚

Author:

Give your best

2 thoughts on “Program Bahasa Java Penjumlahan dan Perkalian Matriks Ordo 2*2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s