Java program to add two matrices – The following Java Code will let you know how to perform two matrix addition using Java. Soon we will add compiler to execute the Program below each method. We have written the Program in 3 Possible ways. Here it is,
- Using For Loop
- Using While
- Using Do-While
Addition of two matrices can be carried if and only if both the matrices are in the same order. In matrix addition, each term of one matrix is added to the other matrix’s term, at the same location, i.e. the term at first row first column of Matrix 1 will be added to the term at first row first column of Matrix 2 and so on. A pictorial example is given below.
Addition Of Two Matrices – Using For Loop
1) If both matrices are of the same size then only we can add the matrices.
2) Use the double dimensional array to store the matrix elements.
3) Read row number,column number and initialize the double dimensional arrays mat1[][],mat2[][],res[][] with same row number,column number.
4) Store the first matrix elements into the two-dimensional array mat1[][] using two for loops. i indicates row number, j indicates column index.Similarly matrix 2 elements in to mat2[][].
5) Add the two matrices using for loop
- for i=0 to i<row
- for j=0 to j<col
- mat1[i][j] + mat2[i][j] and store it in to the matrix res at res[i][j] .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import java.util.Scanner; class AddMatrix { public static void main(String args[]) { int row, col,i,j; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows"); row = in.nextInt(); System.out.println("Enter the number columns"); col = in.nextInt(); int mat1[][] = new int[row][col]; int mat2[][] = new int[row][col]; int res[][] = new int[row][col]; System.out.println("Enter the elements of matrix1"); for ( i= 0 ; i < row ; i++ ) { for ( j= 0 ; j < col ;j++ ) mat1[i][j] = in.nextInt(); System.out.println(); } System.out.println("Enter the elements of matrix2"); for ( i= 0 ; i < row ; i++ ) { for ( j= 0 ; j < col ;j++ ) mat2[i][j] = in.nextInt(); System.out.println(); } for ( i= 0 ; i < row ; i++ ) for ( j= 0 ; j < col ;j++ ) res[i][j] = mat1[i][j] + mat2[i][j] ; System.out.println("Sum of matrices:-"); for ( i= 0 ; i < row ; i++ ) { for ( j= 0 ; j < col ;j++ ) System.out.print(res[i][j]+"\t"); System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Enter the number of rows 2 Enter the number columns 2 Enter the elements of matrix1 1 1 1 1 Enter the elements of matrix2 2 2 2 2 Sum of matrices:- 3 3 3 3 |
Using While Loop
1) For insert values in the matrices mat1, mat2 using loops –
- While loop iterates until i <row
- while iterates until j<column
- insert the value at mat1[i][j] and increases the j value.
- increase the i value.
2) To add matrices –
- while loop iterates until i<row
- while loop iterates until j<column
- add mat1[i][j] + mat2[i][j] and insert the result at res[i][j].
- increase the j value.
- increase the i value.
3) Print res[i][j] using two for loops then we will get the addition of two matrices.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import java.util.Scanner; class AddMatrix { public static void main(String args[]) { int row, col,i,j; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows"); row = in.nextInt(); System.out.println("Enter the number columns"); col = in.nextInt(); int mat1[][] = new int[row][col]; int mat2[][] = new int[row][col]; int res[][] = new int[row][col]; System.out.println("Enter the elements of matrix1"); i=0; while ( i < row) { j=0; while(j < col) { mat1[i][j] = in.nextInt(); j++; } i++; } System.out.println("Enter the elements of matrix2"); i=0; while ( i < row) { j=0; while(j < col) { mat2[i][j] = in.nextInt(); j++; } i++; } i=0; while ( i < row) { j=0; while(j < col) { res[i][j] = mat1[i][j] + mat2[i][j] ; j++; } i++; } System.out.println("Sum of matrices:-"); i=0; while ( i < row) { j=0; while(j < col) { System.out.print(res[i][j]+"\t"); j++; } System.out.println(); i++; } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Enter the number of rows 3 Enter the number columns 3 Enter the elements of matrix1 1 2 3 4 5 6 7 8 9 Enter the elements of matrix2 9 8 7 6 5 4 3 2 1 Sum of matrices:- 10 10 10 10 10 10 10 10 10 |
Using Do-While Loop
1) For Addition of two matrices
- i=0
- do loop
- j=0
- do loop
- mat1[i][j] + mat2[i][j] and insert the result in to res[i][j].
- Increase the j value. checks the condition j<col.
- Increase the i value. Then checks the condition j<row.
2) Print res[][] then we will get the addition of two matrices.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import java.util.Scanner; class AddMatrix { public static void main(String args[]) { int row, col,i,j; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows"); row = in.nextInt(); System.out.println("Enter the number columns"); col = in.nextInt(); int mat1[][] = new int[row][col]; int mat2[][] = new int[row][col]; int res[][] = new int[row][col]; System.out.println("Enter the elements of matrix1"); i=0; do { j=0; do { mat1[i][j] = in.nextInt(); j++; }while(j < col); i++; } while ( i < row); System.out.println("Enter the elements of matrix2"); i=0; do { j=0; do { mat2[i][j] = in.nextInt(); j++; }while(j < col); i++; } while ( i < row); i=0; do { j=0; do { res[i][j] = mat1[i][j] + mat2[i][j] ; j++; }while(j < col); i++; }while ( i < row); System.out.println("Sum of matrices:-"); i=0; do { j=0; do { System.out.print(res[i][j]+"\t"); j++; }while(j < col); System.out.println(); i++; }while(i< row); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Enter the number of rows 3 Enter the number columns 3 Enter the elements of matrix1 1 1 1 1 1 1 1 1 1 Enter the elements of matrix2 9 9 9 9 9 9 9 9 9 Sum of matrices:- 10 10 10 10 10 10 10 10 10 |
More Java Programs: