Here we will see how to Add a matrix in Java ,we will take input from user that how many rows and columns user want to create then with that input we will take elements of matrix, then we will add the elements of a matrix and then show the output.

Example : Demonstrate to How to add Matrix in Java?

package practiseProject;

import java.util.Scanner;

public class Practice

{

public static void main(String[] args)

{

int i,j;

Scanner in=new Scanner(System.in);

System.out.println(“Enter the no. of Rows and Column”);

int r=in.nextInt();

int c=in.nextInt();

int a1[][]=new int[r][c];

int a2[][]=new int[r][c];

int a3[][]=new int[r][c];

System.out.println(“Enter the no. of Fist Matrix”);

for( i=0;i<r;i++)

for(j=0;j<c;j++)

a1[i][j]=in.nextInt();

System.out.print(“Enter the no of Second Matrix \n”);

for(i=0;i<r;i++)

for(j=0;j<c;j++)

a2[i][j]=in.nextInt();

for(i=0;i<r;i++)

for(j=0;j<c;j++)

a3[i][j]=a1[i][j]+a2[i][j];

System.out.println(“Sum of two matrix”);

for(i=0;i<r;i++)

for(j=0;j<c;j++)

System.out.println(“\t”+a3[i][j]);

System.out.println();

}

}

Output:

*Enter the no. of Rows and Column22Enter the no. of Fist Matrix1111Enter the no of Second Matrix1111Sum of two matrix2222*