#include <stdio.
h>
#include <conio.h>
void main()
int A[10][10], B[10][10], C[10][10];
int m, n, p, q, i, j, k;
clrscr();
printf("\n --- Matrix Multiplication ---");
printf("\n Enter the Rows and Columns For Matrix A");
printf("\n Rows and Columns: ");
scanf("%d %d", &m, &n);
printf("\n");
printf("\n Enter the Rows and Columns For Matrix B");
printf("\n Rows and Columns: ");
scanf("%d %d", &p, &q);
printf("\n");
if (n != p || n == 0 || m == 0 || p == 0 || q == 0)
printf("\n Matrix Multiplication is not possible as no. of columns in matrix A does not match the no.
of rows in Matrix B.");
getch();
return;
}
printf("\n Enter the Elements of Matrix A:");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("\n A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
printf("\n");
printf("\n Enter the Elements of Matrix B:");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
printf("\n B[%d][%d]: ", i, j);
scanf("%d", &B[i][j]);
printf("\n");
for (i = 0; i < m; i++)
for (j = 0; j < q; j++)
C[i][j] = 0;
for (k = 0; k < n; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
printf("\n The Result Matrix C is:");
for (i = 0; i < m; i++)
for (j = 0; j < q; j++)
printf(" %d\t", C[i][j]);
printf("\n");
getch();
🔷 Header Files
#include <stdio.h>
#include <conio.h>
stdio.h → used for printf() and scanf()
conio.h → used for clrscr() and getch() (Turbo C++ specific)
🔷 main() Function
void main()
Execution of the program starts from main()
void is used because Turbo C++ supports it
🔷 Variable Declaration
int A[10][10], B[10][10], C[10][10];
int m, n, p, q, i, j, k;
A → Matrix A
B → Matrix B
C → Resultant matrix
m, n → rows and columns of Matrix A
p, q → rows and columns of Matrix B
i, j, k → loop variables
🔷 Clear the Screen
clrscr();
Clears the output screen (Turbo C++)
🔷 Input Dimensions of Matrix A
printf("\n Enter the Rows and Columns For Matrix A");
scanf("%d %d", &m, &n);
Takes number of rows (m) and columns (n) of Matrix A
🔷 Input Dimensions of Matrix B
printf("\n Enter the Rows and Columns For Matrix B");
scanf("%d %d", &p, &q);
Takes number of rows (p) and columns (q) of Matrix B
🔷 Validation Rule
if (n != p || n == 0 || m == 0 || p == 0 || q == 0)
Why this condition?
Matrix multiplication is possible only if
columns of A == rows of B
Also checks for invalid sizes like zero
If condition fails:
printf("Matrix Multiplication is not possible");
getch();
return;
Displays error message
Stops execution
🔷 Input Elements of Matrix A
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &A[i][j]);
}
}
Uses nested loops
Stores elements row-wise into Matrix A
🔷 Input Elements of Matrix B
for (i = 0; i < p; i++)
{
for (j = 0; j < q; j++)
{
scanf("%d", &B[i][j]);
}
}
Similar nested loop
Stores elements of Matrix B
🔷 Matrix Multiplication Logic
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
C[i][j] = 0;
for (k = 0; k < n; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
Explanation:
Outer loop → rows of Matrix A
Middle loop → columns of Matrix B
Inner loop → performs multiplication and addition
Formula used:
C[i][j] = A[i][0]*B[0][j] + A[i][1]*B[1][j] + ...
🔷 Display Resultant Matrix
printf("\n The Result Matrix C is:");
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
printf(" %d\t", C[i][j]);
}
printf("\n");
}
Prints matrix C in row-column format
🔷 Hold Output Screen
getch();
Waits for a key press before exiting
🔑 One-Line Exam Summary
This program reads two matrices, validates the multiplication rule, multiplies them using nested
loops, and displays the resultant matrix.
If you want next, I can:
Give viva questions & answers
Explain this to a school student
Draw matrix multiplication diagram
Provide sample input & output