C Program to Addition of two Matrices.

Addition of two Matrices :

MatrixAddition.c
#define ROWS 3
#define COLS 4
#include<stdio.h>
int main(void)
{
    int i,j;
    int matx1[ROWS][COLS],matx2[ROWS][COLS],matx3[ROWS][COLS];
    printf("Enter Matrix 1 Values(%dx%d) : \n",ROWS,COLS);

    for(i=0; i<ROWS; i++){
        for(j=0; j<COLS; j++){
            scanf("%d",&matx1[i][j]);
        }
    }

    printf("Enter Matrix 2 Values (%dx%d): \n",ROWS,COLS);
    for(i=0; i<ROWS; i++){
        for(j=0; j<COLS; j++){
            scanf("%d",&matx2[i][j]);
        }
    }

    /*Matrix Addition Logic */
    for(i=0; i<ROWS; i++){
        for(j=0; j<COLS; j++){
            matx3[i][j] = matx1[i][j] + matx2[i][j];
        }

    }

    printf("The resultant Matrix 3 is :\n");
    for(i=0; i<ROWS; i++)
    {
        for(j=0; j<COLS; j++)
            printf("%5d",matx3[i][j]);
        printf("\n");
    }
    return 0;
}

Output:

Terminal
Enter Matrix 1 Values(3x4) :
1 3 5 7
2 4 6 8
3 5 7 9
Enter Matrix 2 Values (3x4):
3 5 7 9
2 4 6 8
1 3 5 7
The resultant Matrix 3 is :
4    8   12   16
4    8   12   16
4    8   12   16

Happy Learning 🙂

About the Author:

Avatar
Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment