Java program to perform addition and multiplication on matrix

In this post, we will learn how to perform matrix operations like matrix addition and matrix multiplication of a matrix using java. Let’s start with how to create the matrix in java.

```import java.io.*;
class MatrixDemo
{
public static void main(String args[]) throws IOException
{
int x,y,i,j,k;
System.out.print("Enter the number of rows of matrix: ");
System.out.print("Enter the number of columns of matrix: ");
int a[][] = new int[x][y];
int b[][] = new int[x][y];
int c[][] = new int[x][y];
System.out.println("Enter the elements of first matrix: ");
for(i=0;i<x;i++)
{
for(j=0;j<n;j++)
{
}
}
System.out.println("Enter the elements of second matrix: ");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
}
}
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("Addition of two matrices is: ");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println();
}
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
c[i][j]=0;
for(k=0;k<x;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
System.out.println("Multiplication of two matrices is: ");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println();
}
}
}
```

Program Output:

```Enter the number of rows of matrix: 2
Enter the number of columns of matrix: 2
Enter the elements of first matrix:
1
3
4
5
Enter the elements of second matrix:
6
7
8
9