- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Check Reverse equal Original
- C Add Subtract Multiply Divide
- C Make Calculator
- C Add Digits of Number
- C Calculate Average, Percentage
- C Calculate Arithmetic Mean
- C Calculate Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Add n Numbers
- C Interchange Numbers
- C Reverse a Number
- C Swap Two Numbers
- C Count Positive Negative Zero
- C Find Largest of Two Numbers
- C Find Largest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Area & Perimeter of Square
- C Area & Perimeter of Rectangle
- C Area & Circumference of Circle
- C Convert Fahrenheit to Celsius
- C Convert Celsius to Fahrenheit
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Generate Armstrong Numbers
- C Find nCr and nPr
- C Convert Decimal to Binary
- C Convert Decimal to Octal
- C Convert Decimal to Hexadecimal
- C Convert Binary to Decimal
- C Convert Binary to Octal
- C Convert Binary to Hexadecimal
- C Convert Octal to Decimal
- C Convert Octal to Binary
- C Convert Octal to Hexadecimal
- C Convert Hexadecimal to Decimal
- C Convert Hexadecimal to Binary
- C Convert Hexadecimal to Octal
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Print Smiling face
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Add Two Numbers using Pointer
- C Find Largest Element in Array
- C Find Smallest Element in Array
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C 3D Array Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Delete Vowels from String
- C Delete Word from String
- C Find Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Generate Random Numbers
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C List Files in Directory
- C Delete a File
- C Encrypt & Decrypt a File
- C Print Date
- C Get IP Address
- C Shutdown Computer
- C More Programs
- C Check Palindrome String
- C Inches to Centimetres
- C Kilogram to Gram
- C Count Even Odd
- C Count Vowels Consonants
- C Find Profit Loss
- C Sum All Matrix Elements
- C First & Last Digit Sum
- C Largest Element in Matrix
- C Pass Array to Function
- C Address of Variable
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Reverse File
- C Print Message with Time
- C Print Number in Words
- C Print Successive Character
- C Sum of their Square
- C Replace Vowel in String
- C Sort Words in Ascending
- C Sort Words in Descending
- C Sort String in Alphabetical
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List

« Previous Program Next Program »

In this article, you will learn and get code about the multiplication of two matrix in C. But before going through the program, if you are not aware about how multiplication of two matrix performs, then I recommend you to have a look at the step by step process of matrix multiplication. There, you will see the step by step process using pictorial representation, that how a multiplication of two matrix gets calculated. Now let's move on and implement it in a C program.

To multiply any two matrices in C programming, first ask from the user to enter any two matrix, then start
multiplying the given two matrices, and store the multiplication result one by one inside any variable say
**sum**. Store the value of **sum** in the third matrix (one by one as its element) say **mat3** as shown
in the program given here.

The question is, **write a program in C that multiply two given matrices**. The answer to this question is
given below. This C program asks from user to enter any two 3*3 matrix elements, to multiply them to form a new
matrix which is the multiplication result of two given 3*3 matrices. Here **3*3** matrix means, a matrix
that has 3 rows and 3 columns:

// Multiply two 3*3 Matrices in C // ----codescracker.com---- #include<stdio.h> #include<conio.h> int main() { int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k; printf("Enter first 3*3 matrix element: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) scanf("%d", &mat1[i][j]); } printf("Enter second 3*3 matrix element: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) scanf("%d", &mat2[i][j]); } printf("\nMultiplying two matrices..."); for(i=0; i<3; i++) { for(j=0; j<3; j++) { sum=0; for(k=0; k<3; k++) sum = sum + mat1[i][k] * mat2[k][j]; mat3[i][j] = sum; } } printf("\nMultiplication result of the two given Matrix is: \n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) printf("%d\t", mat3[i][j]); printf("\n"); } getch(); return 0; }

As the above program was written under **Code::Blocks** IDE, therefore after successful build and run, here is the output you
will see on your screen. This is first snapshot:

Now supply any 9 elements for first 3*3 matrix, and then again 9 elements for second 3*3 matrix. After supplying all the 9-9 elements
for both the 3*3 matrix, press `ENTER`

to see the multiplication result of the two matrix as shown in the second screenshot
of the sample run given here:

- Get first 9 elements or numbers from user and store it inside the first matrix, index wise from 00 to 22
- That is first element stored inside mat1[0][0], second element stored inside mat1[0][1], third element stored inside mat1[0][2], ....., seventh element stored inside mat1[2][0], eigth element stored inside mat1[2][1], and the last or ninth element stored inside mat1[2][2]
- In similar way, get second 9 elements from user and store it inside the second matrix
- Now We've total of two 3*3 matrix with 9-9 elements in each.
- Here We've used three for loop to multiply the matrices. The first two for loop is used for row and column, whereas the third one is used to apply the multiplication rule of matrix
- Apply the matrix multiplication rule and multiply the matrix, after getting multiplication result, store the value inside the sum variable, after multiplying each and every row elements of first matrix with corresponding each and every column elements of second matrix, and initialize the value of sum variable into the third matrix one by one. Never forgot to initialize 0 to sum before multiplying process starts for each index of third matrix
- In this way the third matrix say mat3 contains total of 9 elements that will be the multiplication result of the two given matrix say mat1 and mat2
- Finally print the value of third matrix

Now let's modify the above program by implenting extra feature. That is, this program allows user to define the size of matrix:

// Matrix Multiplication Program in C // ----codescracker.com---- #include<stdio.h> #include<conio.h> int main() { int mat1[10][10], mat2[10][10], matmult[10][10]; int row1, col1, row2, col2, i, j, k, sum; printf("Enter size of first matrix:\n"); printf("Enter row size: "); scanf("%d", &row1); printf("Enter column size: "); scanf("%d", &col1); printf("\nEnter the element of first matrix:\n"); for(i=0; i<row1; i++) { for(j=0; j<col1; j++) scanf("%d", &mat1[i][j]); } printf("\nEnter size of second matrix:\n"); printf("Enter row size: "); scanf("%d", &row2); printf("Enter column size: "); scanf("%d", &col2); printf("\nEnter the element of second matrix:\n"); for(i=0; i<row2; i++) { for(j=0; j<col2; j++) scanf("%d", &mat2[i][j]); } if(col1!=row2) { printf("\nMultiplication not possible!"); printf("\nExiting...\n"); printf("Press any key..."); getch(); return 0; } printf("\nMultiplying the two matrix...\n"); for(i=0; i<row1; i++) { for(j=0; j<col2; j++) { sum = 0; for(k=0; k<col1; k++) sum = sum + mat1[i][k] * mat2[k][j]; matmult[i][j] = sum; } } printf("The multiplication result (resultant matrix) is:\n"); for(i=0; i<row1; i++) { for(j=0; j<col2; j++) printf("%d ", matmult[i][j]); printf("\n"); } getch(); return 0; }

Before multiplying the two given matrix entered at run-time, we have applied an if statement to check whether the column size of first matrix is equal to the row size of second matrix or not.

If it is equal, then start multiplying and find out the result. And if it is not, then print some result like
**multiplication not possible!**. Here is the sample run:

The snapshot after providing row and column size along with matrix elements (for first matrix):

The snapshot after providing row and column size along with matrix elements (for second matrix). Because we have provided 3 as row size of second matrix, which is equal to the column size of first matrix:

Now let's take another sample run. Here in this case, let's suppose user has entered the column size of first matrix as 3 and row size of second matrix as 2 which is not equal:

As you can clearly see from the above sample runs, the matrix multiplication is not possible, if the column size of first matrix is not equal to the row size of the second matrix.

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.