How to write a program for Metrix addition,subtraction & multiplication in Java.
Hello Friends,
Today I will give you an example for writeing the java program for Metrix addition, Metrix subtraction and Metrix Multiplication.
Java code:
import java.io.IOException;
public class metrix {
public static void main(String args[])
throws IOException {
int a[][] = {{30,55,29},{54,67,84},{76,89,43}};
int b[][] = {{4,7,5},{2,6,5},{1,4,8}};
int c[][] = new int [3][3];
System.out.println(“THe Matrix A:”);
for(int i=0;i<3 font="" i="">3>
{
for(int j=0;j<3 font="" j="">3>
{
System.out.print(” “+a[i][j]);
}
System.out.println(“n”);
}
System.out.println(“THe Matrix B:”);
for(int i=0;i<3 font="" i="">3>
{
for(int j=0;j<3 font="" j="">3>
{
System.out.print(” “+b[i][j]);
}
System.out.println(“n”);
}
char choise;
System.out.println(“Enter choise: 1)sum ,2)sub ,3)mul. “);
choise = (char)System.in.read();
switch(choise)
{
case ‘1’:
{
for(int i=0;i<3 font="" i="">3>
{
for(int j=0;j<3 font="" j="">3>
{
System.out.print(” “+(a[i][j]+b[i][j]));
}
System.out.println(“n”);
}
break;
}
case ‘2’:
{
for(int i=0;i<3 font="" i="">3>
{
for(int j=0;j<3 font="" j="">3>
{
System.out.print(” “+(a[i][j]-b[i][j]));
}
System.out.println(“n”);
}
break;
}
case ‘3’:
{
for(int i=0;i<3 font="" i="">3>
{
for(int j=0;j<3 font="" j="">3>
{ c[i][j]=0;
for(int k=0;k<3 font="" k="">3>
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
System.out.println(“n”);
}
System.out.println(“Multiplication is:”);
for(int i=0;i<3 font="" i="">3>
{
for(int j=0;j<3 font="" j="">3>
{
System.out.print(” “+c[i][j]);
}
System.out.println(“n”);
}
break;
}
default:
return;
}
}
Output :
}