C Program to Subtract 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 subtracted 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("Subtraction 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
4 5 6
7 8 9
1 2 3
Enter the second matrix
8 4 3
5 7 9
12 4 9
Subtraction of matrices A & B is
-4 1 3
2 1 0
-11 -2 -6