Q. Write a program to multiply two matrices.

Solution :- In this program, two matrices are given, our task is to multiply them. Matrices can be either square or rectangular.

Matrices :- A matrices is ​​a rectangular array of numbers that are arranged in rows and columns.

For Example :-

Input : matrix1[][] = {{1, 2}, 
{3, 4}}
matrix2[][] = {{1, 1}, 
{1, 1}}
Output : {{3, 3}, 
{7, 7}}

Matrices Multiplication Algorithm

START
Step 1 : clare variables and initialize necessary variables
Step 2 : Enter the element of matrices by row wise using loops
Step 3 : Check the number of rows and column of first and second matrices
Step 4 : If number of rows of first matrix is equal to the number of columns of second matrix, go to step 6.Otherwise, print matrix multiplication is not possible and go to step 3.
Step 5 : Multiply the matrices using nested loops.
Step 6 : Print the product in matrix form as console output.
STOP


Matrices Multiplication Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    #define N 4 
    // This function multiplies mat1[][] and mat2[][], 
    // and stores the result in res[][] 
    void matrix_multiply(int matrix1[][N], int matrix2[][N], int result[][N]) 
    { 
    int i, j, k; 
    for (i = 0; i < N; i++) 
    { 
    for (j = 0; j < N; j++) 
    { 
    result[i][j] = 0; 
    for (k = 0; k < N; k++) 
    result[i][j] += matrix1[i][k]*matrix2[k][j]; 
    } 
    } 
    } 
    int main() 
    { 
    int matrix1[N][N] = { {0, 1, 2, 3}, 
    {4, 5, 6, 7}, 
    {8, 9, 10, 11}, 
    {12, 13, 14, 15}}; 
    int matrix2[N][N] = { {1, 2, 3, 4}, 
    {1, 2, 3, 4}, 
    {1, 2, 3, 4}, 
    {1, 2, 3, 4}}; 
    int result[N][N]; // To store result 
    int i, j; 
    matrix_multiply(matrix1, matrix2, result); 
    printf("Result matrix is \n"); 
    for (i = 0; i < N; i++) 
    { 
    for (j = 0; j < N; j++) 
    printf("%d ", result[i][j]); 
    printf("\n"); 
    } 
    return 0; 
    } 
    
    #include <iostream>
    using namespace std; 
    #define N 4 
    void matrix_multiply(int matrix1[][N],  
    int matrix2[][N],  
    int result[][N]) 
    { 
    int i, j, k; 
    for (i = 0; i < N; i++) 
    { 
    for (j = 0; j < N; j++) 
    { 
    result[i][j] = 0; 
    for (k = 0; k < N; k++) 
    result[i][j] += matrix1[i][k] *  
    matrix2[k][j]; 
    } 
    } 
    } 
    // Driver Code 
    int main() 
    { 
    int i, j; 
    int result[N][N]; // To store result 
    int matrix1[N][N] = { {0, 1, 2, 3}, 
    {4, 5, 6, 7}, 
    {8, 9, 10, 11}, 
    {12, 13, 14, 15}}; 
    int matrix2[N][N] = { {1, 2, 3, 4}, 
    {1, 2, 3, 4}, 
    {1, 2, 3, 4}, 
    {1, 2, 3, 4}}; 
    matrix_multiply(matrix1, matrix2, result); 
    cout << "Result matrix is \n"; 
    for (i = 0; i < N; i++) 
    { 
    for (j = 0; j < N; j++) 
    cout << result[i][j] << " "; 
    cout << "\n"; 
    } 
    return 0; 
    } 
    
    class LFC { 
    static int N = 4; 
    static void matrix_multiply(int matrix1[][],  
    int matrix2[][], int result[][]) 
    { 
    int i, j, k; 
    for (i = 0; i < N; i++) 
    { 
    for (j = 0; j < N; j++) 
    { 
    result[i][j] = 0; 
    for (k = 0; k < N; k++) 
    result[i][j] += matrix1[i][k]  
    * matrix2[k][j]; 
    } 
    } 
    } 
    // Driver code 
    public static void main (String[] args)  
    { 
    int matrix1[][] = { {0, 1, 2, 3}, 
    {4, 5, 6, 7}, 
    {8, 9, 10, 11}, 
    {12, 13, 14, 15}}; 
    int matrix2[][] = { {1, 2, 3, 4}, 
    {1, 2, 3, 4}, 
    {1, 2, 3, 4}, 
    {1, 2, 3, 4}}; 
    // To store result 
    int result[][] = new int[N][N] ; 
    int i, j; 
    matrix_multiply(matrix1, matrix2, result); 
    System.out.println("Result matrix"
    + " is "); 
    for (i = 0; i < N; i++) 
    { 
    for (j = 0; j < N; j++) 
    System.out.print( result[i][j] 
    + " "); 
    System.out.println(); 
    } 
    } 
    } 
    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):  
    print(R[i][j],end=" ") 
    print("\n",end="") 
    # First matrix. M is a list 
    M = [[0, 1, 2, 3],  
    [4, 5, 6, 7],  
    [8, 9, 10, 11], 
    [12, 13, 14, 15]] 
    # Second matrix. N is a list 
    N = [[1, 2, 3, 4],  
    [1, 2, 3, 4],  
    [1, 2, 3, 4], 
    [1, 2, 3, 4]]  
    # Call matrix_multiplication function 
    matrix_multiplication(M,N)
    
    using System; 
    class LFC { 
    static int N = 4; 
    static void matrix_multiply(int[,] matrix1,  
    int [,]matrix2, int [,]result) 
    { 
    int i, j, k; 
    for (i = 0; i < N; i++) 
    { 
    for (j = 0; j < N; j++) 
    { 
    result[i,j] = 0; 
    for (k = 0; k < N; k++) 
    result[i,j] += matrix1[i,k]  
    * matrix2[k,j]; 
    } 
    } 
    } 
    // Driver code 
    public static void Main ()  
    { 
    int [,]matrix1 = { {0, 1, 2, 3}, 
    {4, 5, 6, 7}, 
    {8, 9, 10, 11}, 
    {12, 13, 14, 15}};
    int [,]matrix2 = { {1, 2, 3, 4}, 
    {1, 2, 3, 4}, 
    {1, 2, 3, 4}, 
    {1, 2, 3, 4}}; 
    // To store result 
    int [,]result = new int[N,N] ; 
    int i, j; 
    matrix_multiply(matrix1, matrix2, result); 
    Console.WriteLine("Result matrix"
    + " is "); 
    for (i = 0; i < N; i++) 
    { 
    for (j = 0; j < N; j++) 
    Console.Write( result[i,j] 
    + " "); 
    Console.WriteLine(); 
    } 
    } 
    } 
    
    function matrix_multiply(&$matrix1, &$matrix2, &$result) 
    { 
    $N = 4; 
    for ($i = 0; $i < $N; $i++) 
    { 
    for ($j = 0; $j < $N; $j++) 
    { 
    $result[$i][$j] = 0; 
    for ($k = 0; $k < $N; $k++) 
    $result[$i][$j] += $matrix1[$i][$k] * 
    $matrix2[$k][$j]; 
    } 
    } 
    } 
    // Driver Code 
    $matrix1 = array(array(0, 1, 2, 3), 
    array(4, 5, 6, 7), 
    array(8, 9, 10, 11), 
    array(12, 13, 14, 15)); 
    $matrix2 = array(array(1, 2, 3, 4), 
    array(1, 2, 3, 4), 
    array(1, 2, 3, 4), 
    array(1, 2, 3, 4)); 
    matrix_multiply($matrix1, $matrix2, $result); 
    $N = 4; 
    echo ("Result matrix is \n"); 
    for ($i = 0; $i < $N; $i++) 
    { 
    for ($j = 0; $j < $N; $j++) 
    { 
    echo ($result[$i][$j]); 
    echo(" "); 
    } 
    echo ("\n"); 
    } 
    
    #include <stdio.h>
    #define N 4 
    // This function multiplies mat1[][] and mat2[][], 
    // and stores the result in res[][] 
    void matrix_multiply(int matrix1[][N], int matrix2[][N], int result[][N]) 
    { 
    int i, j, k; 
    for (i = 0; i < N; i++) 
    { 
    for (j = 0; j < N; j++) 
    { 
    result[i][j] = 0; 
    for (k = 0; k < N; k++) 
    result[i][j] += matrix1[i][k]*matrix2[k][j]; 
    } 
    } 
    } 
    int main() 
    { 
    int matrix1[N][N] = { {0, 1, 2, 3}, 
    {4, 5, 6, 7}, 
    {8, 9, 10, 11}, 
    {12, 13, 14, 15}}; 
    int matrix2[N][N] = { {1, 2, 3, 4}, 
    {1, 2, 3, 4}, 
    {1, 2, 3, 4}, 
    {1, 2, 3, 4}}; 
    int result[N][N]; // To store result 
    int i, j; 
    matrix_multiply(matrix1, matrix2, result); 
    printf("Result matrix is \n"); 
    for (i = 0; i < N; i++) 
    { 
    for (j = 0; j < N; j++) 
    printf("%d ", result[i][j]); 
    printf("\n"); 
    } 
    return 0; 
    } 
    

    Output

    Result matrix is 
    6  12  18  24
    22 44  66  88
    38 76  114 152
    54 108 162 216
    

    Recommended Programs

       Program to add 2 Matrices
       Program to subtract 2 Matrices