C Program to Add Two Matrices Using Multidimensional Arrays

In this program, the user is asked to enter the elements of the two matrices a and b. Then the elements of these two matrices are added and saved it in third matrix. Finally, the result (third matrix) is printed on the screen.



Source Code
#include<stdio.h>
void main()
{
    int a[3][3], b[3][3], c[3][3], i, j;

    printf("Enter the first matrix\n");
    for(i=0;i <3; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    printf("Enter the second matrix\n");
    for(i=0;i <3; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf("%d", &b[i][j]);
        }
    }
    printf("Addition of matrices A & B is\n");
    for(i=0;i <3; i++)
    {
        for(j=0; j<3; j++)
        {
            c[i][j] = a[i][j] + b[i][j];
            printf("%2d ", c[i][j]);
        }
        printf("\n");
    }
}
Output
Enter the first matrix
1 2 4
4 2 1
8 6 3
Enter the second matrix
1 7 2
5 3 4
8 6 4
Addition of matrices A & B is
 2  9  6
 9  5  5
16 12  7





"Coding Hub - Learn to code" app now available on Google Play Store