//Addition and Multiplication of Two Matrices A and B
import java.io.*;
class MatrixAddition
{
public static void main(String aa[]) throws Exception
{
int A[][]=new int[3][3];
int B[][]=new int[3][3];
int C[][]=new int[3][3];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please Enter 9 Elements in Matrix A:-");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
A[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("Please Enter 9 Elements in Matrix B:-");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
B[i][j]=Integer.parseInt(br.readLine());
}
}
//Addition of A and B
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
System.out.println("Addition of Two Matrices is :-");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(" "+C[i][j]);
}
System.out.println("");
}
//Multiplication of A and B
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
C[i][j]=0;
for(int k=0;k<3;k++)
{
C[i][j]=A[i][k]*B[k][j]+C[i][j];
}
}
}
System.out.println("Multiplication of Two Matrices is :-");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(" "+C[i][j]);
}
System.out.println("");
}
}
}
Output |
No comments:
Post a Comment