import java.util.
Scanner;
public class Matrix{
public static void main(String [] args){
int p,q,m,n,i,j;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number of rows in the first matrix");
p=sc.nextInt();
System.out.print("Enter the number of colums in the first matrix");
q=sc.nextInt();
System.out.print("Enter the number of rows in the second matrix");
m=sc.nextInt();
System.out.print("Enter the number of rows in the second matrix");
n=sc.nextInt();
if (p==m&&q==n){
int a [][]=new int [p][q];
int b [][]=new int [m][n];
int c [][]=new int [p][n];
System.out.println("Enter all the elements of the first matrix");
for (i=0; i<p; i++){
for (j=0; j<q; j++){
a[i][j]=sc.nextInt();
}
}
System.out.println(" ");
System.out.println("Enter all the elements of the second matrix");
for (i=0; i<m; i++){
for (j=0; j<n; j++){
b[i][j]=sc.nextInt();
}
}
System.out.println(" ");
System.out.println("first matrix");
for (i=0; i<p; i++){
for (j=0; j<q; j++){
System.out.print(a[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("second matrix");
for (i=0; i<m; i++){
for (j=0; j<n; j++){
System.out.print(b[i][j]+" ");
}
System.out.println(" ");
}
for (i=0; i<p; i++){
for (j=0; j<n; j++){
c[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("matrix after addition");
for (i=0; i<p; i++){
for (j=0; j<n; j++){
System.out.print(c[i][j]+" ");
}
System.out.println(" ");
}
}
else{
System.out.println("matrix addition not possible, matrices must have same demins
sion");
System.out.println("try again");
}
}
}