program java
Penjumlahan matrik
import java.io.*;
import java.util.*;
public class penjumlahanMatrik
{
public static void main(String[]args)
{
Scanner msklagi= new Scanner(System.in);
Scanner msklagi2= new Scanner(System.in);
DataInputStream entry = new DataInputStream (System.in);
try
{
System.out.print("ketik jumlah baris : ");
int jumlahBaris= Integer.parseInt(entry.readLine());
System.out.print("ketik jumlah kolom : ");
int jumlahKolom= Integer.parseInt(entry.readLine());
int[][] matrikA = new int[jumlahBaris][jumlahKolom];
int[][] matrikB = new int[jumlahBaris][jumlahKolom];
int[][] matrikC = new int[jumlahBaris][jumlahKolom];
for (int i=0;i<jumlahBaris;i++)
for (int j=0;j<jumlahKolom;j++)
{
System.out.print ("masukkan angka untuk matrik A :");
matrikA[i][j] = msklagi.nextInt();
}
for (int i=0;i<jumlahBaris;i++)
for (int j=0;j<jumlahKolom;j++)
{
System.out.print ("masukkan angka untuk matrik B :");
matrikB[i][j] = msklagi2.nextInt();
matrikC[i][j] = matrikA[i][j] + matrikB[i][j];
}
System.out.println("Data Matrik a :");
System.out.println("-------------");
for (int i=0;i<jumlahBaris;i++)
{
for (int j=0;j<jumlahKolom;j++)
System.out.print(matrikA[i][j]+" ");
System.out.println();
}
System.out.println("Data matrik b :");
System.out.println("-------------");
for (int i=0;i<jumlahBaris;i++)
{
for (int j=0;j<jumlahKolom;j++)
System.out.print(matrikB[i][j] + " ");
System.out.println();
}
System.out.println("matrik penjumlahan a + b :");
System.out.println("-------" );
for (int i=0;i<jumlahBaris;i++)
{
for (int j=0;j<jumlahKolom;j++)
System.out.print(matrikC[i][j] + " ");
System.out.println();
}
}
catch (Exception e)
{
System.out.println("salah entry data.");
}
}
}
Perkalian matrik
import java.io.*;
import java.util.*;
public class perkalianMatrik
{
public static void main (String[]args)
{
Scanner msklagi= new Scanner(System.in);
Scanner msklagi2= new Scanner(System.in);
DataInputStream entry = new DataInputStream (System.in);
try
{
System.out.print ("ketik jumlah baris matrik 1 : ");
int jumlahBaris1 = Integer.parseInt(entry.readLine());
System.out.print ("ketik jumlah kolom matrik 1 : ");
int jumlahKolom1 = Integer.parseInt(entry.readLine());
int jumlahBaris2 = jumlahKolom1;
System.out.print ("ketik jumlah kolom matrik 2 : ");
int jumlahKolom2 = Integer.parseInt(entry.readLine());
int[][] matrikA = new int [jumlahBaris1][jumlahKolom1];
int[][] matrikB = new int [jumlahBaris2][jumlahKolom2];
int[][] matrikC = new int [jumlahBaris1][jumlahKolom2];
for (int i=0;i<jumlahBaris1;i++)
for (int j=0;j<jumlahKolom1;j++)
{
System.out.print ("masukkan angka untuk matrik A :");
matrikA[i][j] = msklagi.nextInt();
}
for (int i=0;i<jumlahBaris2;i++)
for (int j=0;j<jumlahKolom2;j++)
{
System.out.print ("masukkan angka untuk matrik B :");
matrikB[i][j] = msklagi2.nextInt();
}
for (int i=0;i<jumlahBaris1;i++)
for (int j=0;j<jumlahKolom2;j++)
{
matrikC[i][j]= 0;
for (int k=0;k<jumlahKolom1;k++)
matrikC[i][j]+= (matrikA[i][k]*matrikB[k][j]);
}
System.out.println("Data matrik A : ");
System.out.println("---------------");
for (int i=0;i<jumlahBaris1;i++)
{
for (int j=0;j<jumlahKolom1;j++)
System.out.print(matrikA[i][j] + " ");
System.out.println();
}
System.out.println("Data matrik B : ");
System.out.println("---------------");
for (int i=0;i<jumlahBaris2;i++)
{
for (int j=0;j<jumlahKolom2;j++)
System.out.print(matrikB[i][j] + " ");
System.out.println();
}
System.out.println("matrik perkalian A dan B : ");
System.out.println("----------------------------");
for (int i=0;i<jumlahBaris1;i++)
{
for (int j=0;j<jumlahKolom2;j++)
System.out.print(matrikC[i][j] + " ");
System.out.println();
}
}
catch (Exception e)
{
System.out.println("salah entry data");
}
}
}