Java Matrix Multiplication

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

Program:

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

public class JavaMatrixMultiplication {

	void printMatrix(int P[][], int r, int c) // Funtion for printing an array
	{
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				System.out.print(P[i][j] + "\t");
			}
			System.out.println();
		}
	}

	public static void main(String args[]) throws Exception {
		JavaMatrixMultiplication ob = new JavaMatrixMultiplication();
		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter no. of rows of 1st Matrix : ");
		int r1 = scanner.nextInt();
		System.out.print("Enter no. of columns of 1st Matrix : ");
		int c1 = scanner.nextInt();

		System.out.print("Enter no. of rows of 2nd Matrix : ");
		int r2 = scanner.nextInt();
		System.out.print("Enter no. of columns of 2nd Matrix : ");
		int c2 = scanner.nextInt();

		if (c1 != r2) // Condition for Multiplication to be possible
		{
			System.out.println("Matrix Multiplication of the given order is not possible");
		} else {
			int A[][] = new int[r1][c1];
			int B[][] = new int[r2][c2];
			int C[][] = new int[r1][c2];

			System.out.println("*************************");
			System.out.println("Inputting the 1st Matrix");
			System.out.println("*************************");
			for (int i = 0; i < r1; i++) {
				for (int j = 0; j < c1; j++) {
					System.out.print("Enter an element : ");
					A[i][j] = scanner.nextInt();
				}
			}
			System.out.println("*************************");
			System.out.println("Inputting the 2nd Matrix");
			System.out.println("*************************");
			for (int i = 0; i < r2; i++) {
				for (int j = 0; j < c2; j++) {
					System.out.print("Enter an element : ");
					B[i][j] = scanner.nextInt();
				}
			}

			/* Matrix Multiplication Starts Here */

			int sum = 0;
			for (int i = 0; i < r1; i++) {
				for (int j = 0; j < c2; j++) {
					for (int k = 0; k < c1; k++) {
						sum = sum + A[i][k] * B[k][j];
					}
					C[i][j] = sum;
					sum = 0;
				}
			}

			System.out.println("n*************************");
			System.out.println("          Output         ");
			System.out.println("*************************");
			System.out.println("The 1st Matrix is");
			ob.printMatrix(A, r1, c1);
			System.out.println("*************************");
			System.out.println("The 2nd Matrix is");
			ob.printMatrix(B, r2, c2);
			System.out.println("************************************");
			System.out.println("The Result of Multiplication is");
			ob.printMatrix(C, r1, c2);
		}
	}
}
                /* ............... END ............... */
        

Output

Enter no. of rows of 1st Matrix : 2
Enter no. of columns of 1st Matrix : 3
Enter no. of rows of 2nd Matrix : 3
Enter no. of columns of 2nd Matrix : 2
*************************
Inputting the 1st Matrix
*************************
Enter an element : 8
Enter an element : 1
Enter an element : 2
Enter an element : -5
Enter an element : 6
Enter an element : 7
*************************
Inputting the 2nd Matrix
*************************
Enter an element : -5
Enter an element : 1
Enter an element : 0
Enter an element : 2
Enter an element : -11
Enter an element : 7

*************************
Output
*************************
The 1st Matrix is
8 1 2
-5 6 7
*************************
The 2nd Matrix is
-5 1
0 2
-11 7
************************************
The Result of Multiplication is
-62 24
-52 56

Tags

Matrix Multiplication, 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 Subtraction 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