Java program for matrix subtraction. The following Java code to subtract two matrices has been written in three different ways. For addition you can check here, The methods are:
- Using For Loop
- Using While Loop
- Using Do-While Loop
The compiler has been added to the program so that you can execute and check it for yourself, along with suitable examples and sample outputs.
Matrix subtraction is carried out between two matrices of the same order. A detailed example is shown as below:
Using For Loop
1) If two matrices have same size then only we can subtract the two matrices.
2) Read row number “row”,column number “col” using scanner class method.
3) Insert elements at mat1[i][j] using two for loops with the structures for ( i=0;i <row;i++ ),for (j= 0 ; j<col;i++ ) , similarly insert elements at mat2[i][j].i indicates row number and j indicates column number.
4) For subtraction of two matrices
- for i=0 to row-1.
- for j=0 to col-1.
- mat1[i][j] – mat2[i][j] and insert the result at res[i][j].
5) Print res[][] matrix to display the subtraction 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 |
import java.util.Scanner; class SUBMatrix { 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("Enter the elements of matrix2"); for ( i= 0 ; i < row ; i++ ) { for ( j= 0 ; j < col ;j++ ) mat2[i][j] = in.nextInt(); } for ( i= 0 ; i < row ; i++ ) for ( j= 0 ; j < col ;j++ ) res[i][j] = mat1[i][j] - mat2[i][j] ; System.out.println("subtract of two 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 |
Enter the number of rows 2 Enter the number columns 3 Enter the elements of matrix1 1 7 8 4 3 1 Enter the elements of matrix2 0 0 0 0 0 0 Sum of matrices:- 1 7 8 4 3 1 |
Using While Loop
1) To insert elements at the matrix
- while loop iterates until i<row
- while loop iterates until j<col
- insert the element at mat1[i][j],
- j value increased by 1.
- i value increased by 1
2) Similarly insert elements at mat2[][].
3)For subtraction of two matrices
- while loop iterates until i<row.
- inner while loop iterates until j<col.
- subtract mat1[i][j] – mat2[i][j] and insert in to the matrix at res[i][j].
- increase the j value by 1.
- increase the i value.
4) Print the two-dimensional array res[][] to display the subtraction 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 82 |
import java.util.Scanner; class SUBMatrix { 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("subtract of two matrices:-"); i=0; while ( i < row) { j=0; while(j < col) { System.out.print(res[i][j]+" "); 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 9 8 7 6 5 4 3 2 1 Enter the elements of matrix2 1 1 1 1 1 1 1 1 1 subtract of two matrices:- 8 7 6 5 4 3 2 1 0 |
Using Do-While Loop
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 82 |
import java.util.Scanner; class SUBMatrix { 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("subtract of two 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 9 8 7 6 5 4 3 2 1 Enter the elements of matrix2 1 1 1 1 1 1 1 1 1 subtract of two matrices:- 8 7 6 5 4 3 2 1 0 |
More Java Programs: