Write a C program for multiplication of two 4 × 4 matrices.

, , 1 comment

 C Program: Multiply Two 4×4 Matrices

#include <stdio.h>


int main() {

    int A[4][4], B[4][4], C[4][4];

    int i, j, k;


    // Input for first matrix A

    printf("Enter elements of first 4x4 matrix (A):\n");

    for(i = 0; i < 4; i++) {

        for(j = 0; j < 4; j++) {

            scanf("%d", &A[i][j]);

        }

    }


    // Input for second matrix B

    printf("Enter elements of second 4x4 matrix (B):\n");

    for(i = 0; i < 4; i++) {

        for(j = 0; j < 4; j++) {

            scanf("%d", &B[i][j]);

        }

    }


    // Multiply matrices A and B, store in C

    for(i = 0; i < 4; i++) {

        for(j = 0; j < 4; j++) {

            C[i][j] = 0;

            for(k = 0; k < 4; k++) {

                C[i][j] += A[i][k] * B[k][j];

            }

        }

    }


    // Display the result matrix C

    printf("Result of multiplication (A x B) is:\n");

    for(i = 0; i < 4; i++) {

        for(j = 0; j < 4; j++) {

            printf("%d\t", C[i][j]);

        }

        printf("\n");

    }


    return 0;

1 comment:

  1. Great explanation on C program multiplication—very helpful for beginners to understand the logic step by step. For those looking to upgrade their skills beyond programming, check out this excellent digital marketing training in Chennai to build a strong career online.

    ReplyDelete