import java.util.*; class LabTask12 { static int i,j,rows,cols; public static void main(String... args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the no.of rows: "); rows=sc.nextInt(); System.out.print("Enter the no.of columns: "); cols=sc.nextInt(); int sum[][]=new int[rows][cols]; System.out.println("Enter first matrix elements:"); int a[][]=new int[rows][cols]; for(i=0;i<rows;i++){ for(j=0;j<cols;j++){ a[i][j]=sc.nextInt(); } } System.out.println("Enter second matrix elements:"); int b[][]=new int[rows][cols]; for(i=0;i<rows;i++){ for(j=0;j<cols;j++){ b[i][j]=sc.nextInt(); } } for(i=0;i<rows;i++){ for(j=0;j<cols;j++){ sum[i][j]=a[i][j]+b[i][j]; } } System.out.println("The resultant matrix is:"); for(i=0;i<rows;i++){ for(j=0;j<cols;j++) System.out.print(sum[i][j]+"\t"); System.out.println(); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter