WAP to enter elements for two 2x2 matrices M and N and display the sum of the matrices.



2x2 matrices M and N and display the sum of the matrices.
#include <stdio.h>
int main() {
    int M[2][2], N[2][2], sum[2][2];
    int i, j;
    // Input elements of matrix M
    printf("Enter elements of matrix M (2x2):\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            printf("M[%d][%d]: ", i, j);
            scanf("%d", &M[i][j]);
        }
    }
    // Input elements of matrix N
    printf("Enter elements of matrix N (2x2):\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            printf("N[%d][%d]: ", i, j);
            scanf("%d", &N[i][j]);
        }
    }
    // Calculate sum of matrices
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            sum[i][j] = M[i][j] + N[i][j];
        }
    }
    // Display sum of matrices
    printf("Sum of matrices M and N:\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Post a Comment

0 Comments