In this C Program for Multiplication of Matrix using the 2D array, we will multiply two matrices A and B, e.g., C=A*B; then, we print matrix C.
The number of columns of matrix A should equal the number of rows of matrix B.
Multiplication of Matrices in C using 2D array
#include <stdio.h>
int main()
{
int C[10][10], A[10][10], B[10][10], i, j, l, row, col;
printf("Enter the number of rows (1-5): ");
scanf("%d", &row);
printf("Enter the number of columns (1-5): ");
scanf("%d", &col);
printf("\n Matrix A \n");
printf("\n Enter element you want to insert in the matrix A: \n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("\t");
scanf("%d", &A[i][j]);
}
}
printf("\n Matrix A:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("%d", A[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n\n Matrix B");
printf("\n Enter element you want to insert in the matrix B:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("\t");
scanf("%d", &B[i][j]);
}
}
printf("\n Matrix B:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("%d", B[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n A*B=\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < row; j++)
{
C[i][j] = 0;
for (l = 0; l < col; l++)
{
C[i][j] = C[i][j] + A[i][l] * B[l][j];
}
printf("%d", C[i][j]);
printf("\t");
}
printf("\n");
}
return 0;
}
Video Solution for Matrix Multiplication in C
Enter the number of rows (1-5): 2
Enter the number of columns (1-5): 2
Matrix A
Enter element you want to insert in the matrix A:
1
2
3
4
Matrix A:
1 2
3 4
Matrix B
Enter element you want to insert in the matrix B:
5
6
7
8
Matrix B:
5 6
7 8
A*B=
19 22
43 50
Matrix Multiplication in C++ using for loop
It is a c++ program for matrix multiplication. We will take input from the user for both the matrices’ rows and columns.
#include<iostream>
using namespace std;
int main()
{
int row1, col1;
cout<<"Enter the number of rows of Matrix A: \n";
cin>>row1;
cout<<"Enter the number of columns of Matrix A: \n";
cin>>col1;
int A[row1][col1];
cout<<"\n Matrix A\n";
cout<<"\n Enter element you want to insert in the matrix A:\n";
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
cin>>A[i][j];
}
}
int row2, col2;
cout<<"Enter the number of rows of Matrix B: \n";
cin>>row2;
cout<<"Enter the number of columns of Matrix B: \n";
cin>>col2;
int B[row2][col2];
cout<<"\n Matrix B \n";
cout<<"\n Enter element you want to insert in the matrix B:\n";
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
cin>>B[i][j];
}
}
if(col1!=row2)
{
exit(0);
}
int C[row1][col2];
cout<<"\n A*B=\n";
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < row1; j++)
{
C[i][j] = 0;
for (int l = 0; l < col2; l++)
{
C[i][j] = C[i][j] + A[i][l] * B[l][j];
}
cout<<C[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}

Approach for Multiplication of two matrices using 2D array:
- Using Nested for loop, we initialize array elements in two matrices, A & B.
- Using Nested for loop, we print all the elements of matrices entered by the user on the screen.
- Using Nested for loop, we multiply two matrices. For this we use the following statement: C[i][j]= C[i][j]+A[i][l]*B[l][j];
About this C Program:
Siddharth Jha writes this program. If you have any doubts regarding the multiplication of matrices in c, let me know in the comment section. Please subscribe to our newsletter, to receive helpful coding blogs and software jobs.