Addmatrix With java


import javax.swing.*;

public class addMatriks {

public static void main (String args[]) {

int [][] matA = new int [2][2];

int [][] matB = new int [2][2];

int [][] matC = new int [2][2];

String input;

String output = “”;

//terima masukan matriksA

for(int i=0;i<matA.length;i++) {

for(int j=0;j<matA.length;j++) {

input = JOptionPane.showInputDialog(null,"matriksA ["+(i+1)+","+(j+1)+"] = \n");

matA[i][j] = Integer.parseInt(input);

}

}

//terima masukan matriksB

for(int i=0;i<matB.length;i++) {

for(int j=0;j<matB.length;j++) {

input = JOptionPane.showInputDialog(null,"matriksB ["+(i+1)+","+(j+1)+"] = \n");

matB[i][j] = Integer.parseInt(input);

}

}

//operasi adding matriks

matC[0][0] = matA[0][0] + matB[0][0];

matC[0][1] = matA[0][1] + matB[0][1];

matC[1][0] = matA[1][0] + matB[1][0];

matC[1][1] = matA[1][1] + matB[1][1];

//cetak matriks ke suatu string

output = output+"Matriks A : \n";

for (int i=0; i<2; i++) {

output = output + "| ";

for (int j=0; j<2; j++) {

output = output +" "+ matA[i][j] + " ";

}

output = output + "|\n";

}

output = output+"Matriks B : \n";

for (int i=0; i<2; i++) {

output = output + "| ";

for (int j=0; j<2; j++) {

output = output +" "+ matB[i][j] + " ";

}

output = output + "|\n";

}

output = output + "\nMatriks C = Matriks A + Matriks B : \n";

for(int i=0;i<2;i++) {

output = output + "| ";

for (int j=0; j<2; j++) {

output = output + " " +matC[i][j] + " ";

}

output = output + "|\n";

}

//cetak matriks asal dan matriks invers ke dialogbox

JOptionPane.showMessageDialog(null,output);

}

}

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