Matrix addition in C
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
First matrix:
1 2
3 4
First matrix:
1 2
3 4
Second matrix:
4 5
-1 5
then the output of the program (Summation of the two matrices) is:
5 7
2 9
-1 5
then the output of the program (Summation of the two matrices) is:
5 7
2 9
Matrices are frequently used in programming to represent graph data structure, in solving equations and in many other ways.
C program for matrix addition:
#include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &i, &j);
printf("Enter the value of first matrix;\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the value of Second matrix;\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Addition of two matrix \n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
}
printf("\n");}
return 0;}
No comments