C program that calculates and displays the multiplication of two matrices.
Here is an example of a C program that calculates the multiplication of two matrices:
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
int main() {
int A[MAX_ROWS][MAX_COLS], B[MAX_ROWS][MAX_COLS], C[MAX_ROWS][MAX_COLS];
int rowsA, colsA, rowsB, colsB;
printf("Enter number of rows and columns for matrix A: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter elements for matrix A: \n");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Enter number of rows and columns for matrix B: ");
scanf("%d %d", &rowsB, &colsB);
printf("Enter elements for matrix B: \n");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
scanf("%d", &B[i][j]);
}
}
// Check if matrices can be multiplied
if (colsA != rowsB) {
printf("Matrices cannot be multiplied.\n");
return 0;
}
// Initialize elements of matrix C to 0
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
C[i][j] = 0;
}
}
// Perform multiplication
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
printf("Result of multiplying matrices A and B: \n");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
Netra Koirala
Computer Science Educator
Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.
LinkedIn ProfileRelated Posts
Loading related posts…
Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.
Featured Post
Grade 10 Computer Science: Specification Grid & Model Questions
Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...