Matrix Operations Implementation In Java
import java.io.*;
class Matrix
{
public static void main(String args[])
{
DataInputStream in=new DataInputStream(System.in);
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
int i,j,k;
try
{
System.out.println("Enter 1st matrix:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j] = Integer.parseInt(in.readLine());
System.out.println("Enter 2nd matrix:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j] = Integer.parseInt(in.readLine());
System.out.println("Enter the option:");
System.out.println("A. Matrix Addition");
System.out.println("B. Matrix subtraction");
System.out.println("C. Matrix Multiplication");
char s = (char)System.in.read();
System.out.println("You entered " + s);
switch(s)
{
case 'A':
case 'a':
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j] = a[i][j]+b[i][j];
break;
case 'B':
case 'b':
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j] = a[i][j]-b[i][j];
break;
case 'C':
case 'c':
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
c[i][j] = c[i][j] + a[i][k]*b[k][j];
break;
default:
System.out.println("\nPlease enter a valid option");
}
System.out.println("Result matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
System.out.println("\t" + c[i][j]);
System.out.println("\n");
}
}
catch(Exception e) {}
}
}
-----------------------------Output--------------------------------------------
--------------------------------------------------------------------------------
Enter 1st matrix:
1 2 3
1 2 3
1 2 3
Enter 2nd matrix:
5 6 7
4 5 6
3 4 5
Enter the option:
A. Matrix Addition
B. Matrix subtraction
C. Matrix Multiplication
A
You entered A
Result matrix:
6 8 10
5 7 9
4 6 8
Enter 1st matrix:
1 2 3
1 2 3
1 2 3
Enter 2nd matrix:
1 2 1
3 4 5
7 9 5
Enter the option:
A. Matrix Addition
B. Matrix subtraction
C. Matrix Multiplication
B
You entered B
Result matrix:
0 0 2
-2 -2 -2
-6 -7 -2
Enter 1st matrix:
1 2 3
1 2 3
1 2 3
Enter 2nd matrix:
4 5 6
4 5 6
4 5 6
Enter the option:
A. Matrix Addition
B. Matrix subtraction
C. Matrix Multiplication
C
You entered C
Result matrix:
24 30 36
24 30 36
24 30 36
class Matrix
{
public static void main(String args[])
{
DataInputStream in=new DataInputStream(System.in);
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
int i,j,k;
try
{
System.out.println("Enter 1st matrix:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j] = Integer.parseInt(in.readLine());
System.out.println("Enter 2nd matrix:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j] = Integer.parseInt(in.readLine());
System.out.println("Enter the option:");
System.out.println("A. Matrix Addition");
System.out.println("B. Matrix subtraction");
System.out.println("C. Matrix Multiplication");
char s = (char)System.in.read();
System.out.println("You entered " + s);
switch(s)
{
case 'A':
case 'a':
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j] = a[i][j]+b[i][j];
break;
case 'B':
case 'b':
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j] = a[i][j]-b[i][j];
break;
case 'C':
case 'c':
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
c[i][j] = c[i][j] + a[i][k]*b[k][j];
break;
default:
System.out.println("\nPlease enter a valid option");
}
System.out.println("Result matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
System.out.println("\t" + c[i][j]);
System.out.println("\n");
}
}
catch(Exception e) {}
}
}
-----------------------------Output--------------------------------------------
--------------------------------------------------------------------------------
Enter 1st matrix:
1 2 3
1 2 3
1 2 3
Enter 2nd matrix:
5 6 7
4 5 6
3 4 5
Enter the option:
A. Matrix Addition
B. Matrix subtraction
C. Matrix Multiplication
A
You entered A
Result matrix:
6 8 10
5 7 9
4 6 8
Enter 1st matrix:
1 2 3
1 2 3
1 2 3
Enter 2nd matrix:
1 2 1
3 4 5
7 9 5
Enter the option:
A. Matrix Addition
B. Matrix subtraction
C. Matrix Multiplication
B
You entered B
Result matrix:
0 0 2
-2 -2 -2
-6 -7 -2
Enter 1st matrix:
1 2 3
1 2 3
1 2 3
Enter 2nd matrix:
4 5 6
4 5 6
4 5 6
Enter the option:
A. Matrix Addition
B. Matrix subtraction
C. Matrix Multiplication
C
You entered C
Result matrix:
24 30 36
24 30 36
24 30 36
You Might Also Like :