Matrices addition:
import java.util.*;
class autoarray
{
public static void main(String a[])
{
int mat1[][],mat2[][],mat3[][];
int rows,cols;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no of
rows:");
rows=sc.nextInt();
System.out.println("Enter the no of
cols:");
cols=sc.nextInt();
mat1=new int[rows][cols];
mat2=new int[rows][cols];
mat3=new int[rows][cols];
System.out.println("Enter the elements
of first matrix:");
for(int
i=0;i<rows;i++)
{
for(int j=0; j<cols;j++)
{
mat1[i][j]=sc.nextInt();
}
}
System.out.println("Enter
the elements of second matrix:");
for(int
i=0;i<rows;i++)
{
for(int j=0; j<cols;j++)
{
mat2[i][j]=sc.nextInt();
}
}
for(int i=0;i<rows;i++)
{
for(int
j=0; j<cols;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
System.out.println("Result is:");
for(int
i=0;i<rows;i++)
{
for(int j=0; j<cols;j++)
{
System.out.print(mat3[i][j]+"
");
}
System.out.println();
}
}
}
Comments
Post a Comment