package array;
import java.util.Scanner;
public class matadd {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int row = s.nextInt();
System.out.print("Enter number of columns: ");
int col = s.nextInt();
int a [][]= new int[row][col];
int b [][]= new int[row][col];
System.out.println("Enter the first matrix");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
a[i][j] = s.nextInt();
}
}
System.out.println("Enter the second matrix");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
b[i][j] = s.nextInt();
}
}
int c [][]= new int[row][col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("The sum of the two matrices is");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class matadd {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int row = s.nextInt();
System.out.print("Enter number of columns: ");
int col = s.nextInt();
int a [][]= new int[row][col];
int b [][]= new int[row][col];
System.out.println("Enter the first matrix");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
a[i][j] = s.nextInt();
}
}
System.out.println("Enter the second matrix");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
b[i][j] = s.nextInt();
}
}
int c [][]= new int[row][col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("The sum of the two matrices is");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}
No comments:
Post a Comment