Matrix

In Mathematics, matrix is a 2-dimensional rectangular array which is filled either with numbers or symbols or expressions as its elements & has a row and column

1) Java Program for Matrix Addition,Subtraction & Multiplication :

import java.util.Scanner;

public class MatrixAddition {
	public static void main(String[] args) {
		System.out.println("Please enter number of rows in the matrix");
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		System.out.println("\nPlease enter number of columns in the matrix");
		int column = sc.nextInt();

		int[][] first_row = new int[row][column];
		int[][] second_row = new int[row][column];

		System.out.println("\n");

		for (int r = 0; r < row; r++) {
			for (int c = 0; c < column; c++) {
				System.out.println(String.format("Enter first[%d][%d] integer", r, c));
				first_row[r][c] = sc.nextInt();
			}
		}

		for (int r = 0; r < row; r++) {
			for (int c = 0; c < column; c++) {
				System.out.println(String.format("Enter second[%d][%d] integer", r, c));
				second_row[r][c] = sc.nextInt();
			}
		}

		sc.close();

		System.out.println("\nFirst Matrix:\n");
		print2dArray(first_row);
		

		System.out.println("\nSecond Matrix:\n");
		print2dArray(second_row);

		matrixaddition(first_row, second_row); // Addition of Matrices
		
		System.out.println("\n");
		
		matrixsubtraction(first_row, second_row); // Subtraction of Matrices
		
		System.out.println("\n");
		
		matrixmultiplication(first_row, second_row); // Multiplication of Matrices
		
	}

	private static void print2dArray(int[][] matrix) {
		for (int r = 0; r < matrix.length; r++) {
			for (int c = 0; c < matrix[0].length; c++) {
				System.out.print(matrix[r][c] + "\t");
			}
			System.out.println();
		}
	}
	
	
	// Function for Addition Of Matrices
	private static void matrixaddition(int[][] first_list, int[][] second_list) {
		int row = first_list.length;
		int column = first_list[0].length;
		int[][] result = new int[row][column];

		for (int r = 0; r < row; r++) {
			for (int c = 0; c < column; c++) {
				result[r][c] = first_list[r][c] + second_list[r][c];
			}
		}

		System.out.println("\nAddition of Matrices is :\n");
		print2dArray(result);
	}
	
	
	// Function for Subtraction Of Matrices
	private static void matrixsubtraction(int[][] first, int[][] second) {
		int row = first.length;
		int column = first[0].length;
		int[][] result = new int[row][column];

		for (int r = 0; r < row; r++) {
			for (int c = 0; c < column; c++) {
				result[r][c] = first[r][c] - second[r][c];
			}
		}

		System.out.println("\nSubtraction of Matrices is :\n");
		print2dArray(result);
	}

	// Function for Multiplication Of Matrices
	private static void matrixmultiplication(int[][] first, int[][] second) {
		int row = first.length;
		int column = first[0].length;
		int[][] result = new int[row][column];

		for (int r = 0; r < row; r++) {
			for (int c = 0; c < column; c++) {
				result[r][c] = first[r][c] * second[r][c];
			}
		}

System.out.println("\nMultiplication of Matrices is :\n");
		print2dArray(result);
	}
}




Output :

Please enter number of rows in the matrix
3

Please enter number of columns in the matrix
3


Enter first[0][0] integer
41
Enter first[0][1] integer
43
Enter first[0][2] integer
46
Enter first[1][0] integer
59
Enter first[1][1] integer
56
Enter first[1][2] integer
58
Enter first[2][0] integer
63
Enter first[2][1] integer
72
Enter first[2][2] integer
83
Enter second[0][0] integer
12
Enter second[0][1] integer
19
Enter second[0][2] integer
21
Enter second[1][0] integer
21
Enter second[1][1] integer
29
Enter second[1][2] integer
26
Enter second[2][0] integer
34
Enter second[2][1] integer
36
Enter second[2][2] integer
30

First Matrix:

41	43	46	
59	56	58	
63	72	83	

Second Matrix:

12	19	21	
21	29	26	
34	36	30	

Addition of Matrices is :

53	62	67	
80	85	84	
97	108	113	


Subtraction of Matrices is :

29	24	25	
38	27	32	
29	36	53	


Multiplication of Matrices is :

492	817	966	
1239	1624	1508	
2142	2592	2490	
Scroll to Top