Java Matrix Subtraction

Chapter: Math Class Last Updated: 18-05-2017 13:18:30 UTC

Program:

            /* ............... START ............... */
                
import java.util.Scanner;

public class JavaMatrixSubtraction {

	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();
		}
	}
}
                /* ............... END ............... */
        

Output

Enter the number of rows and columns of matrix
2
2
Enter the elements of matrix1

1
3
5
6
Enter the elements of matrix2
4
5
6
7
Difference of entered matrices:-
-3	-2	
-1	-1

Notes:

  • 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.

Tags

Matrix Subtraction, Java, Math

Similar Programs Chapter Last Updated
Java Program To Calculate Standard Deviation Math Class 21-09-2017
Java Program To Display Factors Of Number Math Class 12-09-2017
Java Program To Find Power Of Number Using pow() Function Math Class 29-08-2017
Java Program To Find Quotient And Remainder Math Class 25-08-2017
Java Matrix Multiplication Math Class 18-05-2017
Java Matrix Addition Math Class 17-05-2017
LCM And GCD Of Two Numbers In Java Math Class 14-05-2017
Java Cube Root Of Number Math Class 10-03-2017
Java Program To Calculate Area Of Right Triangle Math Class 16-12-2016
Java Program To Find Rectangle Perimeter Math Class 15-06-2016
Java Program To Find Circle Perimeter Math Class 15-06-2016
Math.Floor In Java Example Math Class 14-06-2016
Math.ceil In Java Example Math Class 14-06-2016
Java Program To Find Square Root Of Given Number Math Class 14-06-2016
Random Number Generation In Java Example Math Class 14-06-2016
Quadratic Equation In Java Example Math Class 14-06-2016
Java Program To Displays Powers of 2 Example Math Class 14-06-2016
Java Program To Check For Scalar Matrix Math Class 30-05-2016
Java Program To Find Saddle Point In Square Matrix Math Class 30-05-2016
Decimal To Octal Converstion In Java Math Class 30-05-2016
Decimal To Binary Converstion In Java Math Class 30-05-2016
Java Program To Check For Upper Triangular Matrix Math Class 28-05-2016
Java Program To Check For Pronic Number Math Class 28-05-2016
Java Program To Find Value Of Mobius Function For number Math Class 28-05-2016
Java Program To Check For Lower Triangular Matrix Math Class 28-05-2016
Java Program To Check For Harshad Number Math Class 28-05-2016
Java Program To Check Fascinating Number Math Class 28-05-2016
Java Program To Check For Evil Number Math Class 28-05-2016
Java Program To Check Bouncy Number Increasing Decreasing Number Math Class 07-02-2018
Tower Of Hanoi In Java Example Math Class 05-11-2016

1 2