Program to Calculate 3X3 Matrix Multiplication [C, C++, Python & Java]

Posted in

Program to Calculate 3X3 Matrix Multiplication [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on December 25, 2024

    In this tutorial, we will write a program to calculate 3x3 matrix multiplication in C, C++, Python, and Java. Each program will ask the user to enter the elements for two 3x3 matrices. Each matrix will have 9 elements. The program then multiplies the two matrices and prints the resultant matrix.

    To write the program, you must know what a matrix is, how to multiply two matrices, the programming language syntax, and the for loop.

    So, let us begin without any delay.

    What is a Matrix?

    A matrix is a rectangular array where elements are arranged in rows and columns. The horizontal arrangement of elements is called a row, and the vertical arrangement is called a column.

    If a matrix is represented as [A] m*n, it implies that the matrix has m number of rows and n number of columns. Each number of a matrix is referred to as an element. It is represented as a mn.

    Square Matrix: A matrix is known as a square matrix when its number of rows equals the number of columns.

    To multiply two matrices, the number of rows of the first matrix should be equal to the number of columns of the second matrix.

    For this tutorial, we have taken two 3X3 matrices, i.e., square matrices.

    C Program to Calculate 3x3 Matrix Multiplication

    #include<stdio.h>
    int main()
    {
       int arr1[3][3],arr2[3][3],arr3[3][3];
       int i, j, k;
       printf("Enter the Elements of Array 1: \n");
       for(int i=0;i<3;i++)
       {
           for(int j=0;j<3;j++)
             scanf("%d", &arr1[i][j]);
        }
        printf("Enter the Elements of Array2: \n");
        for(i=0;i<3;i++)
        {
           for(j=0;j<3;j++)
             scanf("%d", &arr2[i][j]);
        }
        printf("Output Matrix:\n");
        for(i=0;i<3;i++)
        {
           for(j=0;j<3;j++)
           {
               arr3[i][j]=0;
               for(k=0;k<3;k++)
                   arr3[i][j]=arr3[i][j]+(arr1[i][k]*arr2[k][j]);
               printf("%d ", arr3[i][j]);
           }
        printf("\n");
        }
        return 0;
    }

    Output:

    Enter the Elements of Array 1: 
    3 4 5 6 1 2 3 7 9
    Enter the Elements of Array2: 
    5 4 3 6 7 8 2 9 1
    Output Matrix:
    49 85 46 
    40 49 28 
    75 142 74 

    C++ Program to Calculate 3x3 Matrix Multiplication

    #include<iostream>
    using namespace std;
    int main()
    {
       int arr1[3][3],arr2[3][3],arr3[3][3];
       cout<<"Enter the Elements of Array 1: "<<endl;
       for(int i=0;i<3;i++)
       {
           for(int j=0;j<3;j++)
             cin>>arr1[i][j];
        }
        cout<<"Enter the Elements of Array2:"<<endl;
        for(i=0;i<3;i++)
        {
           for(int j=0;j<3;j++)
             cin>>arr2[i][j];
        }
        cout<<"Output Matrix:\n";
        for(i=0;i<3;i++)
        {
           for(int j=0;j<3;j++)
           {
               arr3[i][j]=0;
               for(int k=0;k<3;k++)
                   arr3[i][j]=arr3[i][j]+(arr1[i][k]*arr2[k][j]);
               cout<<arr3[i][j]<<" ";
           }
        cout<<endl;
        }
        return 0;
    }

    Output:

    Enter the Elements of Array 1: 
    3 4 5 4 2 1 3 8 9
    Enter the Elements of Array2:
    4 3 2 6 4 1 8 0 9
    Output Matrix:
    76 25 55 
    36 20 19 
    132 41 95 

    Python Program to Calculate 3x3 Matrix Multiplication

    arr1=[[0,0,0],[0,0,0],[0,0,0]]
    arr2=[[0,0,0],[0,0,0],[0,0,0]]
    arr3= [[0,0,0],[0,0,0],[0,0,0]]
    
    print("Enter the Elements of Array 1:")
    for i in range(0,3):
        for j in range(0,3):
            element= int(input())
            arr1[i][j]=element
    
    print("Enter the Elements of Array 2:")
    for i in range(3):
        for j in range(3):
            element2= int(input())
            arr2[i][j]=element2
    
    for i in range(3):
        for j in range(3):
            for k in range(3):
                arr3[i][j]+=arr1[i][k]*arr2[k][j]
                                                   
    print(arr3[0],'\n',arr3[1],'\n',arr3[2])

    Output:

    Enter the Elements of Array 1:
    
    3
    4
    5
    2
    54
    6
    23
    45
    12
    
    Enter the Elements of Array 2:
    
    23
    34
    54
    5
    76
    65
    34
    76
    85
    [259, 786, 847]
    [520, 4628, 4128]
    [1162, 5114, 5187]

    Java Program to Calculate 3x3 Matrix Multiplication

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            int[][] a = new int[3][3];
            int[][] b = new int[3][3];
            int[][] c = new int[3][3];
            int i, j, k;
            Scanner sc = new Scanner(System.in);
    
            System.out.println("Enter the Elements of Array 1: \n");
            for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
    
            System.out.println("Enter the Elements of Array2: \n");
            for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                    b[i][j] = sc.nextInt();
                }
            }
    
            System.out.println("Output Matrix:\n");
            for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                    c[i][j] = 0;
                    for (k = 0; k < 3; k++) {
                        c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
                    }
                    System.out.print(" " + c[i][j]);
                }
                System.out.println();
            }
        }
    }

    Output:

    Enter the Elements of Array 1: 
    
    3 4 5 2 1 4 5 6 7
    Enter the Elements of Array2: 
    
    3 7 6 2 9 8 1 3 4
    Output Matrix:
    
     22 72 70
     12 35 36
     34 110 106

    Conclusion

    Matrix multiplication is a common endeavor in scientific and mathematical programming. Hence, every programmer must be aware of it. In this tutorial, we have provided code capable of multiplying only a 3x3 matrix. You can design the program for other square matrices to learn the concept better, like 4x4 and 5x5 matrices.

    Moreover, you can create a program to multiply square matrices of any size. However, doing so will require much knowledge and effort. Let us know if you can do so.

    People are also reading:

    Leave a Comment on this Post

    0 Comments