Matrix Addition in C

Here you will find program for matrix addition in C.

Two matrix can be added only when number of rows and columns of first matrix is equal to number of rows of columns of second matrix.

Matrix Addition in C

#include<stdio.h>

int main()
{
	int a[5][5],b[5][5],c[5][5],i,j,m,n;
	printf("How many rows and columns?");
	scanf("%d%d",&m,&n);
	printf("\nEnter first matrix:\n");
	
	for(i=0;i<m;++i)
		for(j=0;j<n;++j)
			scanf("%d",&a[i][j]);
			
	printf("\nEnter second matrix:\n");
	
	for(i=0;i<m;++i)
		for(j=0;j<n;++j)
			scanf("%d",&b[i][j]);
	
	printf("\nMatrix after addition:\n");
	
	for(i=0;i<m;++i)
	{
		for(j=0;j<n;++j)
		{
			c[i][j]=a[i][j]+b[i][j];
			printf("%d ",c[i][j]);
		}
		
		printf("\n");
	}

	return 0;
}

 

Output

How many rows and columns?3
3

Enter first matrix:
2 6 9
3 2 0
2 4 1

Enter second matrix:
3 4 1
6 7 9
11 3 5

Matrix after addition:
5 10 10
9 9 9
13 7 6

2 thoughts on “Matrix Addition in C”

Leave a Comment

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