Share this post on:

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 Column
2
2
Enter the no. of Fist Matrix
1
1
1
1
Enter the no of Second Matrix
1
1
1
1
Sum of two matrix
2
2
2
2

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *