Java Program to Perform Matrix Multiplication

This is a java program to perform a simple matrix multiplication. For matrix multiplication to happen the column of the first matrix should be equal to the row of the second matrix.

Here is the source code of the Java Program to Perform Matrix Multiplication. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

// This is sample program for matrix multiplication
// The complexity of the algorithm is O(n^3)
package com.sanfoundry.numerical;
 
import java.util.Scanner;
 
public class MatixMultiplication
{
    public static void main(String args[])
    {
        int n;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the base of squared matrices");
        n = input.nextInt();
        int[][] a = new int[n][n];
        int[][] b = new int[n][n];
        int[][] c = new int[n][n];
        System.out.println("Enter the elements of 1st martix row wise \n");
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                a[i][j] = input.nextInt();
            }
        }
        System.out.println("Enter the elements of 2nd martix row wise \n");
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                b[i][j] = input.nextInt();
            }
        }
        System.out.println("Multiplying the matrices...");
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < n; k++)
                {
                    c[i][j] = c[i][j] + a[i][k] * b[k][j];
                }
            }
        }
        System.out.println("The product is:");
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                System.out.print(c[i][j] + " ");
            }
            System.out.println();
        }
        input.close();
    }
}

Output:

$ javac MatixMultiplication.java
$ java MatixMultiplication
 
Enter the base of squared matrices:
3
Enter the elements of 1st martix row wise:
1 2 3
4 5 6
7 8 9
Enter the elements of 2nd martix row wise:
2 3 4
5 6 7
8 9 1
Multiplying the matrices...
The product is:
36 42 21
81 96 57
126 150 93

Related posts:

Converting Strings to Enums in Java
Vòng lặp for, while, do-while trong Java
Java Program to Implement Cubic convergence 1/pi Algorithm
Java Program to Find Transpose of a Graph Matrix
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
How to Find an Element in a List with Java
Java 9 Stream API Improvements
Java Program to Implement Circular Doubly Linked List
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Implement Self organizing List
Spring 5 Testing with @EnabledIf Annotation
HttpClient Timeout
Guide to java.util.Formatter
A Guide to TreeSet in Java
Spring Boot Actuator
Java Program to Perform Stooge Sort
Java Program to Implement EnumMap API
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Apache Commons Collections BidiMap
Spring WebClient and OAuth2 Support
HashSet trong Java hoạt động như thế nào?
Hướng dẫn Java Design Pattern – Interpreter
Comparing Objects in Java
Spring Security Authentication Provider
HTTP Authentification and CGI/Servlet
Java Program to Implement Graph Coloring Algorithm
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Guava Collections Cookbook
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Java Program to Implement Nth Root Algorithm
Spring Security 5 – OAuth2 Login
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach