Matrix Subtraction Example in Java
On this page (9sections)
Introduction
Matrix Subtraction is a classic Java console program that demonstrates the concept with complete source code and sample output. Calculation programs apply formulas to solve geometry, statistics and numeric problems.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Definition
A matrix (plural matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns that is treated in certain prescribed ways. Matrix subtraction is the operation of subtracting two matrices by subtracting the corresponding entries together. Two matrices must have an equal number of rows and columns to be subtracted. The difference between two matrices A and B will be a matrix which has the same number of rows and columns as do A and B. The difference of A and B, denoted A - B, is computed by subtracting corresponding elements of A and B.
Matrix Subtraction Example Program
import java.util.Scanner;
class MatrixSubtraction{
public static void main(String args[]){
int rows,columns, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
rows= in.nextInt();
columns = in.nextInt();
int matrix1[][] = new int[rows][columns];
int matrix2[][] = new int[rows][columns];
int diff[][] = new int[rows][columns];
System.out.println("Enter the elements of matrix1");
for ( c = 0 ; c < rows ; c++ ){
for ( d = 0 ; d < columns ; d++ ){
matrix1[c][d] = in.nextInt();
}
}
System.out.println("Enter the elements of matrix2");
for ( c = 0 ; c < rows ; c++ ){
for ( d = 0 ; d < columns ; d++ ){
matrix2[c][d] = in.nextInt();
}
}
for ( c = 0 ; c < rows ; c++ ){
for ( d = 0 ; d < columns ; d++ ){
diff[c][d] = matrix1[c][d] - matrix2[c][d];
}
}
System.out.println("Difference of entered matrices:-");
for ( c = 0 ; c < rows ; c++ ){
for ( d = 0 ; d < columns ; d++ ){
System.out.print(diff[c][d]+"\t");
}
System.out.println();
}
}
}
Sample Output
Enter the number of rows and columns of matrix
3
3
Enter the elements of matrix1
8
6
2
8
5
4
7
3
2
Enter the elements of matrix2
9
5
8
2
4
1
7
6
5
Difference of entered matrices:-
-1 1 -6
6 1 3
0 -3 -3
When to use
Use these formulas in homework tools, engineering calculators or anywhere repeated numeric computation is needed.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
import java.util.Scanner;imports a class used later in the program. -
A
Scannerreads typed input from the keyboard (System.in). -
A
println/printcall writes text to the console — part of the sample output below. -
rows= in.nextInt();updates a variable used in the calculation or output. -
columns = in.nextInt();updates a variable used in the calculation or output. -
int matrix1[][] = new int[rows][columns];updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below.
Best Practices
- Use meaningful variable and class names that describe their purpose.
- Compile and run the program locally — modify values to see how output changes.
- Read compiler errors carefully; they usually point to the exact line to fix.
Common Mistakes
- Copying code without understanding each line — practice by changing one statement at a time.
- Mismatching the public class name and the
.javafilename. - Forgetting semicolons at the end of statements.