# C program for Sum of Two Matrix

Before going to the program first let is understand what is Sum of two Matrix?

Sum of two matrix:

Sum of two Matrix is a matrix obtained by adding the corresponding elements of the two given matrix.

But to perform the Sum of two Matrix the number of rows and columns of two matrix must be equal.

Thus the Sum matrix will have same number of rows and columns as the two given matrix. For example, ## Program code for Sum of two Matrix in C:

```#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,i,j,row,col;
clrscr();

printf("\n  Enter the row and column size : ");
scanf("%d %d",&row,&col);

printf("\n  Enter the matrix 1 :\n\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("\n  Enter the matrix 2 :\n\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&b[i][j]);
}
}

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

printf("\n  The sum is : \n\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("  %d ",c[i][j]);
}
printf("\n");
}
getch();
}
```

## Working:

• First the computer reads the rows and columns of matrix 1 and matrix 2 from the user.
• Then using for loops the computer reads the matrix 1 and matrix 2 from the user.
• Then using for loops the matrix addition is carried out.
• Finally using for loops the Sum matrix is printed.

## Step by step working of the above program:

1. Let us assume that a user enters the number of rows and columns as 3 and 3 respectively.
2.  And then enters the elements of matrix 1 and matrix 2 as given below

Matrix 1:  1 1 1

1 1 1

1 1 1

Matrix 2:  2 2 2

2 2 2

2 2 2

1. Then it assigns the value of i=0 and j=0 and the loop continues till condition of the for loop is true.

3.1.   i<row    (0<3)    for loop condition is true

3.1.1.   j<col     (0<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c=a[o][o]+b[o][o])    So  c=3

j++     (j=j+1)    So  j=1

3.1.2.   j<col     (1<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c=a[o]+b[o])    So  c=3

j++     (j=j+1)    So  j=2

3.1.3.   j<col     (2<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c=a[o]+b[o])    So  c=3

j++     (j=j+1)    So  j=3

3.1.4.   j<col     (3<3)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j.

i++     (i=i+1)    So   i=1   ,   j=0

3.2.   i<row    (1<3)    for loop condition is true

3.2.1.   j<col     (0<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c=a[o]+b[o])    So  c=3

j++     (j=j+1)    So  j=1

3.2.2.   j<col     (1<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c=a+b)    So  c=3

j++     (j=j+1)    So  j=2

3.2.3.   j<col     (2<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c=a+b)    So  c=3

j++     (j=j+1)    So  j=3

3.2.4.   j<col     (3<3)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j.

i++     (i=i+1)    So   i=2   ,   j=0

3.3.   i<row    (2<3)    for loop condition is true

3.2.1.   j<col     (0<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c=a[o]+b[o])    So  c=3

j++     (j=j+1)    So  j=1

3.2.2.   j<col     (1<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c=a+b)    So  c=3

j++     (j=j+1)    So  j=2

3.2.3.   j<col     (2<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c=a+b)    So  c=3

j++     (j=j+1)    So  j=3

3.2.4.   j<col     (3<3)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j.

i++     (i=i+1)    So   i=3   ,   j=0

3.4.   i<row    (3<3)    for loop condition is false

It comes out of the for loop.

1. Finally using two for loops it prints the elements of the Sum matrix as given below:

Sum matrix: 3 3 3

3 3 3

3 3 3

1. Thus the program execution is completed.

## Output:  