Wednesday , January 17 2018
Home / c-program / C Program – Addition of two Matrices Example

C Program – Addition of two Matrices Example

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

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

Happy Learning 🙂

About chandrashekhar

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+ ,

Recommended

C Program to add elements of an array

How to add elements in an array using C Program. Add elements of an Array …

Leave a Reply

Your email address will not be published. Required fields are marked *