import java.io.*;
public class MatrixAddition
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][], b[][], c[][];
// Read Number of Rows and Columns of Matrix
System.out.print("Enter Number of Rows of matrix:: ");
int m = Integer.parseInt(br.readLine());
System.out.print("Enter Number of Columns of matrix:: ");
int n = Integer.parseInt(br.readLine());
a = new int[m][n];
int i, j;
// Read First Matrix Elements From User
System.out.println("\nEnter First Matrix Data::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("Enter Element:: ");
a[i][j] = Integer.parseInt(br.readLine());
}
}
b = new int[m][n];
// Read Second Matrix Elements From User
System.out.println("\nEnter Second Matrix Data::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("Enter Element:: ");
b[i][j] = Integer.parseInt(br.readLine());
}
}
// Perform Matrix Addition
c = new int[m][n];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
c[i][j] = a[i][j] + b[i][j];
// Display Result Matrix
System.out.println("\nResultant Matrix::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("\t" + c[i][j]);
}
System.out.println();
}
}
}
public class MatrixAddition
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][], b[][], c[][];
// Read Number of Rows and Columns of Matrix
System.out.print("Enter Number of Rows of matrix:: ");
int m = Integer.parseInt(br.readLine());
System.out.print("Enter Number of Columns of matrix:: ");
int n = Integer.parseInt(br.readLine());
a = new int[m][n];
int i, j;
// Read First Matrix Elements From User
System.out.println("\nEnter First Matrix Data::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("Enter Element:: ");
a[i][j] = Integer.parseInt(br.readLine());
}
}
b = new int[m][n];
// Read Second Matrix Elements From User
System.out.println("\nEnter Second Matrix Data::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("Enter Element:: ");
b[i][j] = Integer.parseInt(br.readLine());
}
}
// Perform Matrix Addition
c = new int[m][n];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
c[i][j] = a[i][j] + b[i][j];
// Display Result Matrix
System.out.println("\nResultant Matrix::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("\t" + c[i][j]);
}
System.out.println();
}
}
}
OUTPUT::
Enter Number of Rows of matrix:: 2
Enter Number of Columns of matrix:: 2
Enter First Matrix Data::
Enter Element:: 1
Enter Element:: 2
Enter Element:: 3
Enter Element:: 4
Enter Second Matrix Data::
Enter Element:: 5
Enter Element:: 6
Enter Element:: 7
Enter Element:: 8
Resultant Matrix::
6 8
10 12