C Examples

WAP in C to read two 3*3 matrix and multiply there values and store them in third matrix!

The Program in C to read two 3*3 matrix and multiply there values and store them in third matrix is given below:

#include <stdio.h>

int main() {
    int a[3][3], b[3][3], c[3][3], i, j, k, sum = 0;

    printf("Enter elements of first 3x3 matrix: \n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    printf("Enter elements of second 3x3 matrix: \n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            scanf("%d", &b[i][j]);
        }
    }

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            for (k = 0; k < 3; k++) {
                sum = sum + a[i][k]*b[k][j];
            }
            c[i][j] = sum;
            sum = 0;
        }
    }

    printf("Product of the matrices: \n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", c[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Enter elements of first 3×3 matrix:
1
9
5
7
8
4
5
6
7
Enter elements of second 3×3 matrix:
8
9
7
5
3
7
8
9
5
Product of the matrices:
93 81 95
128 123 125
126 126 112

Pro-Tips💡

This program uses two nested for loops to read the elements of the first 3×3 matrix from the user, and another two nested loops to read the elements of the second 3×3 matrix.

Then, the program uses three nested for loops to multiply the corresponding elements of the first and second matrices and store the result in the third matrix.


The outer two loops iterate over the rows and columns of resultant matrix and inner loop iterates over the columns of first matrix and rows of second matrix and sums the product of corresponding elements of first and second matrix.

Finally, the program uses two nested for loops again to print the elements of the third matrix which is the product of the first and second matrix.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X