import java.io.DataInputStream;
class MatrixMultiplication
{
public static void main(String[] args)
{
DataInputStream input = new DataInputStream(System.in);
int[][] A = new int[3][3];
int[][] B = new int[3][3];
int[][] C = new int[3][3];
//====================try block in get the data
try
{
System.out.println("Enter elements for matrix A : ");
for (int i=0 ; i < A.length ; i++)
for (int j=0 ; j < A[i].length ; j++)
{
A[i][j] = Integer.parseInt(input.readLine());
}
System.out.println("Enter elements for matrix B : ");
for (int i=0 ; i < B.length ; i++)
for (int j=0 ; j < B[i].length ; j++)
{
B[i][j] = Integer.parseInt(input.readLine());
}
System.out.println("Matrix A: ");
}
//====================try block is over and start the catch block
//====================if error occur then catch block are handle
catch(Exception e)
{
// System.out.println("please try again");
}
//====================catch block over
//start the for loop to print the array
for (int i=0 ; i < A.length ; i++)
{
System.out.println();
for (int j=0 ; j < A[i].length ; j++)
{
System.out.print(A[i][j]+" ");
}
}
System.out.println("Matrix B: "); for (int i=0 ; i < B.length ; i++)
{
System.out.println();
for (int j=0 ; j < B[i].length ; j++)
{
System.out.print(B[i][j]+" ");
}
}
// array is printed
System.out.println("The multiplication of two matrix are given below ");
//multiply this array by using this formula.
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
C[i][j]+=A[i][k]*B[k][j];
}
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(+C[i][j]+" ");
}
System.out.println();
}
//formula is over.
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
//addition of this matrix are given below
System.out.println("the addition of two matrix are given below"); for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(+C[i][j]+" ");
}
System.out.println();
}
}
}
0 comments:
Post a Comment