Today we will learn the C Program for Matrix Multiplication and also learn C Program Matrix Multiplication. But before starting use should have a little bit knowledge about Matrix Multiplication. So,

## What is Matrix Multiplication?

Matrix Multiplication is an operation that results in a new Matrix by Multiplying two Matrix. To multiply matrix we do the dot product of two matrices.

## Algorithm For Matrix Multiplication

1 Step: START.

2 Step: Enter row and column of matrix A.

3 Step: Similarly Enter for Second Matrix B.

4 Step: Enter the elements of Matrix A.

5 Step: Similarly Enter elements of Matrix B.

6 Step: Print elements of Matrix A and B in Matrix form.

7 Step: Set an Outer loop for the row.

8 Step: Set Inner loop for the column.

9 Step: Now Multiply first(A) and second(B) matrix and store in third matrix C.

10 Step: Now print the final Matrix.

11 Step: STOP.

There are different methods to do Matrix Multiplication we will see it one by one.

## 1. C program for Matrix Multiplication

In this program first, we will ask the user how many rows and columns you want for matrix A and B then the program will ask the user to enter the elements and then the compiler will print the multiplication of Matrix A and B in Matrix C.

```#include <stdio.h>
void main()
{
int A[50][50],B[50][50],C[50][50],i,j,k,r,s;
int m,n;
printf("How many rows and columns for first matrix\n");
scanf("%d%d",&m,&n);
printf("How many rows and columns for second matrix\n");
scanf("%d%d",&r,&s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
{
printf("\n Enter the elements of first matrix ");
for(i= 0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&A[i][j]);
}
printf("\n Enetr the elements of second matrix ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&B[i][j]);
}
printf("\n The element of first matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",A[i][j]);
}
printf("\n The element of second matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",B[i][j]);
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
C[i][j]=0;
for(k=0;k<m;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
}
}
printf("\n Multiplication of two matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",C[i][j]);
}
}```

Output:

Working of Matrix Multiplication:

At first, we initialize the variables and array in main and then ask the user for rows and columns for Matrix A and Matrix B.

When the user enters the rows and columns then compiler asks to enter the elements for Matrix A and Matrix B.Then matrix addition is done and the addition is stored in third matrix C.

Finally the matrix is printed on the screen.

## 2. Scalar Matrix Multiplication

In this program, the compiler asks the user to enter row and column for matrix and then the user enters the elements. After the elements are entered the compiler asks to enter the scalar(the number which will be multiplied with the entered matrix).

Now the program performs its operation and prints the final matrix.

```#include<stdio.h>
int main()
{
int i, j, rows, columns, Multiplication[10][10], Number;
printf("\n Please Enter Number of rows and columns\n");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \t");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &Multiplication[rows][columns]);
}
}
printf("\n Please Enter the Multiplication Value  :  ");
scanf("%d", &Number);
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
Multiplication[rows][columns] = Number * Multiplication[rows][columns];
}
}
printf("\n The Result of a Scalar Matrix Multiplication is : \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
printf("%d \t ", Multiplication[rows][columns]);
}
printf("\n");
}
return 0;
}```

Output:

## 3. Multiplication of Matrix Using Function

```#include <stdio.h>
void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2);
void multiplyMatrices(int first[][10], int second[][10], int multResult[][10], int r1, int c1, int r2, int c2);
void display(int mult[][10], int r1, int c2);
int main() {
int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);
// Taking input until columns of the first matrix is equal to the rows of the second matrix
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}
// Function to take matrices data
enterData(first, second, r1, c1, r2, c2);
// Function to multiply two matrices.
multiplyMatrices(first, second, mult, r1, c1, r2, c2);
// Function to display resultant matrix after multiplication.
display(mult, r1, c2);
return 0;
}
void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2) {
printf("\nEnter elements of matrix 1:\n");

for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c1; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &first[i][j]);
}
}
printf("\nEnter elements of matrix 2:\n");
for (int i = 0; i < r2; ++i) {
for (int j = 0; j < c2; ++j) {
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%d", &second[i][j]);
}
}
}
void multiplyMatrices(int first[][10], int second[][10], int mult[][10], int r1, int c1, int r2, int c2) {

// Initializing elements of matrix mult to 0.
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
mult[i][j] = 0;
}
}

// Multiplying first and second matrices and storing in mult.
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
mult[i][j] += first[i][k] * second[k][j];
}
}
}
}

void display(int mult[][10], int r1, int c2) {

printf("\nOutput Matrix:\n");
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
printf("%d  ", mult[i][j]);
if (j == c2 - 1)
printf("\n");
}
}```

Output:

## 3. Multiplication of Square Matrix

In this program, the elements are declared in the program.

```#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;
}
```

Output:

## 4. Multiplication of Square Matrix

```#include<stdio.h>
void multiply(int m1, int m2, int mat1[][m2],
int n1, int n2, int mat2[][n2])
{
int x, i, j;
int res[m1][n2];
for (i = 0; i < m1; i++)
{
for (j = 0; j < n2; j++)
{
res[i][j] = 0;
for (x = 0; x < m2; x++)
{
*(*(res + i) + j) += *(*(mat1 + i) + x) *
*(*(mat2 + x) + j);
}
}
}
for (i = 0; i < m1; i++)
{
for (j = 0; j < n2; j++)
{
printf("%d ", *(*(res + i) + j));
}
printf("\n");
}
}
int main()
{
int mat1[][2] = { { 1, 2 }, { 8, 4 } };
int mat2[][2] = { { 3, 4 }, { 1, 5 } };
int m1 = 2, m2 = 2, n1 = 2, n2 = 2;
multiply(m1, m2, mat1, n1, n2, mat2);
return 0;
}
```

Output: