2 dimensional matrix addition in c program
Matrix addition in C
Matrix addition in C: C program to add two matrices, i.e., compute the sum of two matrices and then print it. Firstly a user will be asked to enter the order of matrix (number of rows and columns) and then two matrices. For example, if a user input order as 2, 2, i.e., two rows and two columns and matrices as
Required knowledge :
➤ for loop
Input:
Enter the row : 2
Enter the col : 2
Enter the elament of frist matrix :
2 3
4 5
Enter the elament of 2nd matrix :
3 4
8 9
output :
sum of matrix :
5 7
12 14
Addition of two matrix code :
int main()
{
int row,col,i,j;
int ss[100][100];
int ar[100][100];
int c[100][100];
printf("Enter the row : ");
scanf("%d",&row);
printf("Enter the col :");
scanf("%d",&col);
printf("Enter the elament of frist matrix :\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("ar[%d][%d]=",i,j);
scanf("%d",&ar[i][j]);
}
printf("\n");
}
printf("\n");
printf("ar matrix = \n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d ",ar[i][j]);
}
printf("\n");
}
printf("Enter the elament of 2nd matrix :\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("\nss[%d][%d]= ",i,j);
scanf("%d",&ss[i][j]);
}
printf("\n");
}
printf("\n");
printf("ss matrix = \n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d ",ss[i][j]);
}
printf("\n");
}
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
c[i][j]=ar[i][j]+ss[i][j];
}
printf("\n");
}
printf("sum of matrix : \n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}
Output :

No comments