C Program to Add Two Matrices

Write a C program for addition of two matrices. This c program accepts two matrices and performs addition operation on them.

C Program for Addition of Two Matrices

/* Aim: Write a program to find additon of two matrices*/

#include<stdio.h>

int main()
{ 
	int A[5][5],B[5][5],C[5][5],r,c,i,j;

	printf("\n Please enter size of matrix A & B  : row, col :- ");
	scanf("%d%d",&r,&c);

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
		printf(" Please enter value of A[%d][%d] ",i,j);
		scanf("%d",&A[i][j]);
		} 
	}

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
		printf(" Please enter value of B[%d][%d] ",i,j);
		scanf("%d",&B[i][j]);
		}
	}

	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
		C[i][j] = A[i][j] + B[i][j];
		}
	}

	printf("\n \n Matrix  A  :  \n");
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
		printf("\t %d",A[i][j]);  
		}
	printf("\n");
	}

	printf("\n \n Matrix  B  :  \n");
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
		printf("\t %d",B[i][j]);  
		}
	printf("\n");
	}

	printf("\n \n Matrix  C  :  \n");
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
		printf("\t %d",C[i][j]);  
		}
	printf("\n");
	}

	return 0;
}

Output:

 Please enter size of matrix A & B  : row, col :- 3 3
 Please enter value of A[0][0] 1
 Please enter value of A[0][1] 5
 Please enter value of A[0][2] 2
 Please enter value of A[1][0] 4
 Please enter value of A[1][1] 5
 Please enter value of A[1][2] 8
 Please enter value of A[2][0] 4
 Please enter value of A[2][1] 8
 Please enter value of A[2][2] 4
 Please enter value of B[0][0] 5
 Please enter value of B[0][1] 8
 Please enter value of B[0][2] 7
 Please enter value of B[1][0] 5
 Please enter value of B[1][1] 8
 Please enter value of B[1][2] 4
 Please enter value of B[2][0] 6
 Please enter value of B[2][1] 4
 Please enter value of B[2][2] 8

 
 Matrix  A  :  
  1  5  2
  4  5  8
  4  8  4

 
 Matrix  B  :  
  5  8  7
  5  8  4
  6  4  8

 
 Matrix  C  :  
  6  13  9
  9  13  12
  10  12  12