c program to implement matrix addition

Description


This program demonstrates matrix addition. Three 2d matrices are declared a[][], b[][] and s[][]. The row and column values of the two matrix a and b is read into the variables r and c. Then the values of the two matrices is read as input by7 using for loop.  The statement s[i][j]=a[i][j]+b[i][j]; inside the for loop performs matrix addition and the statement printf("%d ",s[i][j]); inside the for loop prints the new matrix as the output.


Program


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int r,c,a[50][50],b[50][50],s[50][50],i,j;
printf("Enter the row value and cloum value\n");
scanf("%d%d",&r,&c);
printf("Enter the elements of first matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}}
printf("Enter the elements of second matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
s[i][j]=a[i][j]+b[i][j];
}}
printf("Sum of matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",s[i][j]);
}
printf("\n");
}
getch();
}

No comments: