Program To Multiply Two Matrices

Share Your Love

The task here to multiply 2 matrices either be square or rectangular.

Examples: 

Input : mat1[][] = {{1, 2}, 
                   {3, 4}}
        mat2[][] = {{1, 1}, 
                    {1, 1}}
Output : {{3, 3}, 
          {7, 7}}
Input : mat1[][] = {{2, 4}, 
                    {3, 4}}
        mat2[][] = {{1, 2}, 
                    {1, 3}}       
Output : {{6, 16}, 
          {7, 18}}
matrix multiplication
Matrix Multiplication

Multiplication of Square Matrices : 

The below program multiplies two square matrices of size 4*4, we can change N for different dimensions. This program is written in form of C++.

// C++ program to multiply
// two square matrices.
#include <iostream>

using namespace std;

#define N 4

// This function multiplies
// mat1[][] and mat2[][], and
// stores the result in res[][]
void multiply(int mat1[][N],
			int mat2[][N],
			int res[][N])
{
	int i, j, k;
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			res[i][j] = 0;
			for (k = 0; k < N; k++)
				res[i][j] += mat1[i][k] * mat2[k][j];
		}
	}
}

// Driver Code
int main()
{
	int i, j;
	int res[N][N]; // To store result
	int mat1[N][N] = { { 1, 1, 1, 1 },
					{ 2, 2, 2, 2 },
					{ 3, 3, 3, 3 },
					{ 4, 4, 4, 4 } };

	int mat2[N][N] = { { 1, 1, 1, 1 },
					{ 2, 2, 2, 2 },
					{ 3, 3, 3, 3 },
					{ 4, 4, 4, 4 } };

	multiply(mat1, mat2, res);

	cout << "Result matrix is \n";
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++)
			cout << res[i][j] << " ";
		cout << "\n";
	}

	return 0;
}

C Program To Multiply Two Square Matrices:

// C program to multiply two square matrices.
#include <stdio.h>
#define N 4

// This function multiplies mat1[][] and mat2[][],
// and stores the result in res[][]
void multiply(int mat1[][N], int mat2[][N], int res[][N])
{
	int i, j, k;
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			res[i][j] = 0;
			for (k = 0; k < N; k++)
				res[i][j] += mat1[i][k] * mat2[k][j];
		}
	}
}

int main()
{
	int mat1[N][N] = { { 1, 1, 1, 1 },
					{ 2, 2, 2, 2 },
					{ 3, 3, 3, 3 },
					{ 4, 4, 4, 4 } };

	int mat2[N][N] = { { 1, 1, 1, 1 },
					{ 2, 2, 2, 2 },
					{ 3, 3, 3, 3 },
					{ 4, 4, 4, 4 } };

	int res[N][N]; // To store result
	int i, j;
	multiply(mat1, mat2, res);

	printf("Result matrix is \n");
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++)
			printf("%d ", res[i][j]);
		printf("\n");
	}

	return 0;
}

Java Program To Multiply Two Square Matrices:

// Java program to multiply two square
// matrices.
import java.io.*;

class GFG {

	static int N = 4;

	// This function multiplies mat1[][]
	// and mat2[][], and stores the result
	// in res[][]
	static void multiply(int mat1[][],
						int mat2[][], int res[][])
	{
		int i, j, k;
		for (i = 0; i < N; i++) {
			for (j = 0; j < N; j++) {
				res[i][j] = 0;
				for (k = 0; k < N; k++)
					res[i][j] += mat1[i][k]
								* mat2[k][j];
			}
		}
	}

	// Driver code
	public static void main(String[] args)
	{
		int mat1[][] = { { 1, 1, 1, 1 },
						{ 2, 2, 2, 2 },
						{ 3, 3, 3, 3 },
						{ 4, 4, 4, 4 } };

		int mat2[][] = { { 1, 1, 1, 1 },
						{ 2, 2, 2, 2 },
						{ 3, 3, 3, 3 },
						{ 4, 4, 4, 4 } };

		// To store result
		int res[][] = new int[N][N];
		int i, j;
		multiply(mat1, mat2, res);

		System.out.println("Result matrix"
						+ " is ");
		for (i = 0; i < N; i++) {
			for (j = 0; j < N; j++)
				System.out.print(res[i][j]
								+ " ");
			System.out.println();
		}
	}
}

// This code is contributed by anuj_67.

4×4 matrix multiplication using Python3 Using Function:

# 4x4 matrix multiplication using Python3
# Function definition
def matrix_multiplication(M, N):
	# List to store matrix multiplication result
	R = [[0, 0, 0, 0],
		[0, 0, 0, 0],
		[0, 0, 0, 0],
		[0, 0, 0, 0]]

	for i in range(0, 4):
		for j in range(0, 4):
			for k in range(0, 4):
				R[i][j] += M[i][k] * N[k][j]

	for i in range(0, 4):
		for j in range(0, 4):
			# if we use print(), by default cursor moves to next line each time,
			# Now we can explicitly define ending character or sequence passing
			# second parameter as end ="<character or string>"
			# syntax: print(<variable or value to print>, end ="<ending with>")
			# Here space (" ") is used to print a gape after printing
			# each element of R
			print(R[i][j], end =" ")
		print("\n", end ="")

# First matrix. M is a list
M = [[1, 1, 1, 1],
	[2, 2, 2, 2],
	[3, 3, 3, 3],
	[4, 4, 4, 4]]

# Second matrix. N is a list
N = [[1, 1, 1, 1],
	[2, 2, 2, 2],
	[3, 3, 3, 3],
	[4, 4, 4, 4]]
	
# Call matrix_multiplication function
matrix_multiplication(M, N)

# This code is contributed by Santanu

C# Program to multiply two square matrices:

// C# program to multiply two square
// matrices.
using System;

class GFG {

	static int N = 4;

	// This function multiplies mat1[][]
	// and mat2[][], and stores the result
	// in res[][]
	static void multiply(int[, ] mat1,
						int[, ] mat2, int[, ] res)
	{
		int i, j, k;
		for (i = 0; i < N; i++) {
			for (j = 0; j < N; j++) {
				res[i, j] = 0;
				for (k = 0; k < N; k++)
					res[i, j] += mat1[i, k]
								* mat2[k, j];
			}
		}
	}

	// Driver code
	public static void Main()
	{
		int[, ] mat1 = { { 1, 1, 1, 1 },
						{ 2, 2, 2, 2 },
						{ 3, 3, 3, 3 },
						{ 4, 4, 4, 4 } };

		int[, ] mat2 = { { 1, 1, 1, 1 },
						{ 2, 2, 2, 2 },
						{ 3, 3, 3, 3 },
						{ 4, 4, 4, 4 } };

		// To store result
		int[, ] res = new int[N, N];
		int i, j;
		multiply(mat1, mat2, res);

		Console.WriteLine("Result matrix"
						+ " is ");
		for (i = 0; i < N; i++) {
			for (j = 0; j < N; j++)
				Console.Write(res[i, j]
							+ " ");
			Console.WriteLine();
		}
	}
}

// This code is contributed by anuj_67.

PHP Program To Multiply Two Matrices:

<?php
// PHP program to multiply two
// square matrices.

// This function multiplies mat1[][] and
// mat2[][], and stores the result in res[][]
function multiply(&$mat1, &$mat2, &$res)
{
	$N = 4;
	for ($i = 0; $i < $N; $i++)
	{
		for ($j = 0; $j < $N; $j++)
		{
			$res[$i][$j] = 0;
			for ($k = 0; $k < $N; $k++)
				$res[$i][$j] += $mat1[$i][$k] *
								$mat2[$k][$j];
		}
	}
}

// Driver Code
$mat1 = array(array(1, 1, 1, 1),
			array(2, 2, 2, 2),
			array(3, 3, 3, 3),
			array(4, 4, 4, 4));

$mat2 = array(array(1, 1, 1, 1),
			array(2, 2, 2, 2),
			array(3, 3, 3, 3),
			array(4, 4, 4, 4));

multiply($mat1, $mat2, $res);
$N = 4;
echo ("Result matrix is \n");
for ($i = 0; $i < $N; $i++)
{
	for ($j = 0; $j < $N; $j++)
	{
		echo ($res[$i][$j]);
		echo(" ");
	}
	echo ("\n");
}

// This code is contributed
// by Shivi_Aggarwal
?>

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about this topic discussed above.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter