C Program For Addition Of Two Matrices

In this article, you will learn the steps and syntax to add 2 Matrices using C Programming language.

#include <stdio.h>
#include <conio.h>
int main()
{
	int a_matrix[3][3],b_matrix[3][3],c_matrix[3][3],p,q;
	
	printf("\nENTER MATRIX A elements:\n");
	for(p=0;p<3;p++)
                 for(q=0;q<3;q++)
            	scanf("%d",&a_matrix[p][q]);

	printf("\nENTER MATRIX B elements:\n");
	for(p=0;p<3;p++)
		for(q=0;q<3;q++)
			scanf("%d",&b_matrix[p][q]);
	for(p=0;p<3;p++)
		for(q=0;q<3;q++)
			c_matrix[p][q]=a_matrix[p][q]+b_matrix[p][q];
	printf("\nAddition of Matrix A and Matrix B is MATRIX C and its elements are:\n");
	for(p=0;p<3;p++)
            {
		for(q=0;q<3;q++)
			printf("%6d",c_matrix[p][q]);
		printf("\n");
	}
	return(0);
}


Output:

ENTER MATRIX A elements:

1

2

3

4

5

6

7

8

9

ENTER MATRIX B elements:

1

2

3

4

5

6

7

8

9

Addition of Matrix A and Matrix B is MATRIX C and its elements are:

     2     4     6

     8    10    12

    14    16    18


Code Analysis
Take input for Matrix A and Matrix B. Number of rows of Matrix A and Matrix B are equal. Matrix A and Matrix B are added using the following formula:
Matrix C = Matrix A[ i , j ] + Matrix B[ i , j ] 
Matrix C is displayed.