Embed presentation
Downloaded 15 times
![//Ques. 6 WAP to add two given matrices.
importjava.util.Scanner;
classAddTwoMatrix
{
public static void main(String args[])
{
intm,n,c,d;
Scanner in = new Scanner(System.in);
System.out.print("nEnter the number of rows and columns of matrix = ");
m= in.nextInt();
n= in.nextInt();
int first[][]= new int[m][n];
int second[][]=new int[m][n];
int sum[][]= new int[m][n];
System.out.println("nEnter the elements of first matrix = ");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
first[c][d]= in.nextInt();
System.out.println("nEnter the elements of second matrix = ");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
second[c][d]= in.nextInt();](https://image.slidesharecdn.com/addtitionoftwomatrix-131127074140-phpapp02/75/WAP-to-add-two-given-matrices-in-Java-1-2048.jpg)
![for(c=0;c<m;c++)
for(d=0;d<n;d++)
{
sum[c][d]= first[c][d] + second[c][d];
}
System.out.println("nSum Of Entered Matrices");
for(c=0;c<2;c++)
{
System.out.print("n");
for(d=0;d<2;d++)
{
System.out.print("t"+sum[c][d]);
}
}
System.out.println();
}
}](https://image.slidesharecdn.com/addtitionoftwomatrix-131127074140-phpapp02/75/WAP-to-add-two-given-matrices-in-Java-2-2048.jpg)


The document contains code to add two matrices of given size. It takes the number of rows and columns as input to create two matrices of that size. It then takes elements of both matrices as input and stores them. It iterates through both matrices to add the elements and stores the result in a third sum matrix. Finally, it prints the sum matrix.
![//Ques. 6 WAP to add two given matrices.
importjava.util.Scanner;
classAddTwoMatrix
{
public static void main(String args[])
{
intm,n,c,d;
Scanner in = new Scanner(System.in);
System.out.print("nEnter the number of rows and columns of matrix = ");
m= in.nextInt();
n= in.nextInt();
int first[][]= new int[m][n];
int second[][]=new int[m][n];
int sum[][]= new int[m][n];
System.out.println("nEnter the elements of first matrix = ");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
first[c][d]= in.nextInt();
System.out.println("nEnter the elements of second matrix = ");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
second[c][d]= in.nextInt();](https://image.slidesharecdn.com/addtitionoftwomatrix-131127074140-phpapp02/75/WAP-to-add-two-given-matrices-in-Java-1-2048.jpg)
![for(c=0;c<m;c++)
for(d=0;d<n;d++)
{
sum[c][d]= first[c][d] + second[c][d];
}
System.out.println("nSum Of Entered Matrices");
for(c=0;c<2;c++)
{
System.out.print("n");
for(d=0;d<2;d++)
{
System.out.print("t"+sum[c][d]);
}
}
System.out.println();
}
}](https://image.slidesharecdn.com/addtitionoftwomatrix-131127074140-phpapp02/75/WAP-to-add-two-given-matrices-in-Java-2-2048.jpg)
