//Create a Java program to add two matrices

 


import java.util.Scanner;
public class CDM_P6 {
public static void main(String[] args) {
//Add of two Matrices
int r, c, i, j;
Scanner sc = new Scanner(System.in);

System.out.println("Enter the no of rows");
r = sc.nextInt();

System.out.println("Enter the no of columns");
c = sc.nextInt();

int [][]mat1 = new int[r][c];
int [][]mat2 = new int[r][c];
int [][]result = new int[r][c];

System.out.println("Enter the element of matrix1");
for(i=0; i<r; i++){
for(j=0; j<c; j++)
mat1[i][j] = sc.nextInt()
;
System.out.println();
}
System.
out.println("Enter the element of matrix2");
for(i=0; i<r; i++){
for(j=0; j<c; j++)
mat2[i][j] = sc.nextInt()
;
System.out.println();
}
for(i=0; i<r; i++)
for(j=0; j<c; j++)
result[i][j] = mat1[i][j] + mat2[i][j]
;
System.out.println("Sum of matrices-");
for(i=0; i<r; i++) {
for (j = 0; j < c; j++)
System.
out.print(result[i][j] + "\t");
System.out.println();
}
}
}

//output
Enter the no of rows 2 Enter the no of columns 2 Enter the element of matrix1 4 5 4 7 Enter the element of matrix2 8 9 6 7 Sum of matrices- 12 14 10 14 Process finished with exit code 0



Post a Comment

0 Comments