Matrix Multiplication in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include<stdio.h>

int main() {
    int a[10][10], b[10][10], c[10][10]={0}, n, i, j, k;
    scanf("%d", &n);
    for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d", &a[i][j]);
    for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d", &b[i][j]);
    for(i=0;i<n;i++) for(j=0;j<n;j++)
        for(k=0;k<n;k++) c[i][j] += a[i][k] * b[k][j];
    for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%d ", c[i][j]); printf("\n"); }
}

C Output

Input:  
2  
1 2  
3 4  
5 6  
7 8  

Output:  
19 22  
43 50


C++ Program

#include<iostream>
using namespace std;

int main() {
    int a[10][10], b[10][10], c[10][10]={0}, n;
    cin >> n;
    for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin >> a[i][j];
    for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin >> b[i][j];
    for(int i=0;i<n;i++) for(int j=0;j<n;j++)
        for(int k=0;k<n;k++) c[i][j] += a[i][k] * b[k][j];
    for(int i=0;i<n;i++) { for(int j=0;j<n;j++) cout << c[i][j] << " "; cout << "\n"; }
}

C++ Output

Input:  
2  
2 3  
4 1  
1 2  
3 4  

Output:  
11 16  
7 12


JAVA Program

import java.util.*;

class M {
    public static void main(String[] a) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), i, j, k;
        int[][] A = new int[n][n], B = new int[n][n], C = new int[n][n];
        for(i=0;i<n;i++) for(j=0;j<n;j++) A[i][j] = sc.nextInt();
        for(i=0;i<n;i++) for(j=0;j<n;j++) B[i][j] = sc.nextInt();
        for(i=0;i<n;i++) for(j=0;j<n;j++)
            for(k=0;k<n;k++) C[i][j] += A[i][k]*B[k][j];
        for(i=0;i<n;i++) { for(j=0;j<n;j++) System.out.print(C[i][j]+" "); System.out.println(); }
    }
}

JAVA Output

Input:  
2  
1 0  
0 1  
9 8  
7 6  

Output:  
9 8  
7 6


Python Program

n = int(input())
a = [list(map(int,input().split())) for _ in range(n)]
b = [list(map(int,input().split())) for _ in range(n)]
c = [[sum(a[i][k]*b[k][j] for k in range(n)) for j in range(n)] for i in range(n)]
for row in c: print(*row)

Python Output

Input:  
2  
1 2  
3 4  
5 6  
7 8  

Output:  
19 22  
43 50


In-Depth Learning – Entire Concept in Paragraphs
What Is Matrix Multiplication?
Matrix multiplication is a basic operation in linear algebra in which two matrices are multiplied to yield a third matrix. It's not merely element-by-element multiplication — it's the dot product of rows and columns. This operation is crucial in fields such as computer graphics, physics simulations, AI, machine learning, and data transformation.

How the Code Works
Let's take both matrices A and B as n x n square matrices for ease of explanation. 

To compute every element of the resultant matrix C[i][j], we:

Take the i-th row of matrix A.

Take the j-th column of matrix B.

Multiply the corresponding elements and add them.

In code:

c[i][j] += a[i][k] * b[k][j];
This loop structure:

Outer loops: Traverse for rows and columns.

Inner loop: Does the dot product operation.

Example
Let's multiply:

A =
Copy
Edit
1 2
3 4
B =
Copy
Edit
5 6
7 8
Now calculate C = A x B:

C[0][0] = (1×5 + 2×7) = 5 + 14 = 19

C[0][1] = (1×6 + 2×8) = 6 + 16 = 22

C[1][0] = (3×5 + 4×7) = 15 + 28 = 43

C[1][1] = (3×6 + 4×8) = 18 + 32 = 50

Result:


19 22
43 50
Real-Life Analogy
Suppose you're ordering at a restaurant:

Matrix A: Amounts of items purchased by individual customers.

Matrix B: Prices for those items in various currencies.

By their multiplication, you have the total amount paid by each customer in every currency. That is precisely how matrix multiplication gathers facts through multiplication and addition.

Where and When Is It Used?
Matrix multiplication plays a critical role in:

Machine Learning: Neural network weight matrices

Computer Graphics: Coordinate and shape transformations

Physics/Engineering: Rotations, equations, and simulations

Cryptography & Data Science: Data transformations, linear systems

It's also a favorite in:

Coding interviews

Competitive programming

Academic assignments

Time Complexity
Time Complexity: O(n³) for basic matrix multiplication

Space Complexity: O(n²) for the resulting matrix

Optimizations such as Strassen's Algorithm and block matrix multiplication provide lower time complexity for complex cases.

Python-Specific Advantage
Python's list comprehensions keep matrix multiplication very compact and easy to read. For instance:


[[sum(a[i][k]*b[k][j] for k in range(n)) for j in range(n)] for i in range(n)]
This one-liner accomplishes what three levels of nested loops accomplish — and so it's beautiful for students.

SEO-Optimized Natural Paragraph to Rank
Learn shortest and simplest working code in C, C++, Java, and Python to do matrix multiplication. Matrix multiplication is among the most essential programming and mathematical operations. It drives the computations in artificial intelligence, machine learning, computer graphics, and so on. This step-by-step tutorial makes it easier for beginners to grasp the fundamental logic of multiplying two matrices through nested loops and dot products. Whether you are getting ready for an interview, an exam, or actual usage, learning matrix multiplication will solidify your data structure, algorithm, and problem-solving foundation.