C program to print sum of two matrices

Example#1:

#include<stdio.h>
int main()
{
	int n,i,j,a[30][30],b[30][30],c[30][30];
	printf("Enter the size of the array:");
	scanf("%d",&n);
	printf("\nEnter elements of first matrix:");
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	printf("\nEnter elements of second matrix:");
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			scanf("%d",&b[i][j]);
		}
	}
	printf("\nfrist matrix:\n");
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			printf("%d ",a[i][j]);
		}
		printf("\n");
	}
	printf("\nsecond matrix:\n");
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			printf("%d ",b[i][j]);
		}
		printf("\n");
	}
	printf("\nSum of those matrices:\n");
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			c[i][j]=a[i][j]+b[i][j];
		}
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		
		{
			printf("%d ",c[i][j]);
		}
		printf("\n");
	}
	return 0;
}

Output:

Facebook Comments

Back to Top