Matrix Addition
E · easyP · Verified PYQmatrix
Problem
Given two N x M matrices A and B of the same size, compute and print their sum matrix C where C[i][j] = A[i][j] + B[i][j].
Example
Input
2 2 1 2 3 4 1 1 1 1
Output
2 3 4 5
Read both matrices. Use nested loops: C[i][j] = A[i][j] + B[i][j]. Print row by row.
javamay contain transcription errors
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int r=sc.nextInt(), c=sc.nextInt();
int[][] a=new int[r][c];
int[][] b=new int[r][c];
int[][] sum=new int[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
a[i][j]=sc.nextInt();
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
b[i][j]=sc.nextInt();
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
sum[i][j]=a[i][j]+b[i][j];
System.out.print(sum[i][j]+" ");
}
System.out.println();
}
}
}