# Substraction OF MATRIX ELEMENTS USING METHOD
//NOTE:To add matrix elements, rows and coloumns of both matrices must be same.
import java.util.Scanner;
public class SubMatrix {
void Matrix()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter matrix rows");
int m = s.nextInt();
System.out.println("Enter matrix coloumn");
int n = s.nextInt();
int a[][] = new int[m][n];
int b[][] = new int[m][n];
int c[][] = new int[m][n];
System.out.println("Enter 1st matrix elements..");
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
a[i][j]=s.nextInt();
}
System.out.println("Enter 2nd matrix elements..");
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
b[i][j]=s.nextInt();
}
System.out.println("Substraction of two Matrices");
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
c[i][j]=a[i][j]-b[i][j];
}
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
System.out.print(c[i][j] +" ");
System.out.println();
}
}
public static void main(String[] args)
{
SubMatrix obj = new SubMatrix();
obj.Matrix();
}
}
OUTPUT
Enter matrix rows
2
Enter matrix coloumn
2
Enter 1st matrix elements..
9
8
7
6
Enter 2nd matrix elements..
4
3
2
1
Substraction of two Matrices
5 5
5 5
# ADDITION OF MATRIX ELEMENTS without METHOD(not taking input from user)
public class mAdd {
public static void main(String[] args)
{
int matrix1[][] = {{7,8,9},{6,5,4},{1,2,3}};
int matrix2[][] = {{1,2,3},{4,5,6},{7,8,9}};
int m=matrix1.length;
int n=matrix1[0].length;
int add[][]=new int[m][n];
System.out.println("First matrix elements");
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
System.out.print(matrix1[i][j]+" ");
System.out.println();
}
System.out.println("Second matrix elements");
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
System.out.print(matrix2[i][j]+" ");
System.out.println();
}
System.out.println("Addition of two matrices");
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
add[i][j]=matrix1[i][j]+matrix2[i][j];
}
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
System.out.print(add[i][j] +" ");
System.out.println();
}
}
}
OUTPUT
First matrix elements
7 8 9
6 5 4
1 2 3
Second matrix elements
1 2 3
4 5 6
7 8 9
Addition of two matrices
8 10 12
10 10 10
8 10 12
# MULTIPLICATION OF MATRIX ELEMENTS USING METHOD(taking input from user)
//NOTE:To Multiply matrix elements, rows and coloumns of both matrices must be same.
import java.util.*;
public class MulMatrix {
void mulMatrix()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter matrix rows");
int m = s.nextInt();
System.out.println("Enter matrix coloumn");
int n = s.nextInt();
int a[][] = new int[m][n];
int b[][] = new int[m][n];
int c[][] = new int[m][n];
System.out.println("Enter 1st matrix elements..");
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
a[i][j]=s.nextInt();
}
System.out.println("Enter 2nd matrix elements..");
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
b[i][j]=s.nextInt();
}
System.out.println("Multiplication of two Matrices");
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
{
c[i][j]=0;
for(int k=0; k<n; k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
for(int i=0; i<m;i++)
{
for(int j=0; j<n;j++)
System.out.print(c[i][j] +" ");
System.out.println();
}
}
public static void main(String[] args)
{
MulMatrix obj = new MulMatrix();
obj.mulMatrix();
}
}
OUTPUT
Enter matrix rows
3
Enter matrix coloumn
3
Enter 1st matrix elements..
1
2
3
4
5
6
7
8
9
Enter 2nd matrix elements..
9
8
7
6
5
4
3
2
1
Multiplication of two Matrices
30 24 18
84 69 54
138 114 90