WAP to add or to multiply two matrices of order nXn.


#include <stdio.h>

int main() {
    // Write C code here
    int n1, n2, i, j, m1[10][10], m2[10][10], sum[10][10], mul[10][10], k;
    printf("Enter the order of the matrix: ");
    scanf("%d%d", &n1, &n2);
    printf("Enter the elements of the 1st matrix: ");
    for (i=0; i<n1; i++) {
        for (j=0; j<n2; j++) {
            scanf("%d", &m1[i][j]);
        }
    }
    printf("Enter the elements of the 2st matrix: ");
    for (i=0; i<n1; i++) {
        for (j=0; j<n2; j++) {
            scanf("%d", &m2[i][j]);
        }
    }
    for (i=0; i<n1; i++) {
        for (j=0; j<n2; j++) {
            sum[i][j] = m1[i][j] + m2[i][j];
        }
    }
    printf("The sum of Matrix is: \n");
    for (i=0; i<n1; i++) {
        for (j=0; j<n2; j++) {
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }
    for (i=0; i<n1; i++) {
        for (j=0; j<n2; j++) {
            mul[i][j] = 0;
            for (k=0; k<n1; k++) {
                mul[i][j] += m1[i][k] * m2[k][j];
            }
        }
    }
    printf("Multiple of Matrix is: \n");
    for (i=0; i<n1; i++) {
        for (j=0; j<n2; j++) {
            printf("%d ", mul[i][j]);
        }
        printf("\n");
    }
    
    
    return 0;
}

Comments

Popular posts from this blog

WAP that inputs two arrays and saves sum of corresponding elements of these arrays in a third array

WAP to find the minimum and maximum element of the array by using functions.