C Program – Addition of two Matrices Example

By | 2017-09-15T04:20:45+00:00 September 15th, 2017|c-program|0 Comments

C Program to Addition of two Matrices.

Addition of two Matrices :


#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;
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

Enter Matrix 1 Values(3×4) :
1 3 5 7
2 4 6 8
3 5 7 9
Enter Matrix 2 Values (3×4):
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

[/box]

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment