C Program To Add Two Matrices
Uncategorized

C Program To Add Two Matrices

In this article, we are going to teach you how to write a C Program To Add Two Matrices using multi-dimensional or 2D arrays. We recently published an article Sum of the numbers using functions in c. So, let’s start creating this interesting program without wasting the time.

C Program To Add Two Matrices:

You have to simply write the standard input output library and in the second statement, we define the values of rows and columns before the main function. #define is defined in stdio.h library. Now the values of rows and columns will remain constant throughout the program. The value of rows and columns is 3 and 3. Now simply declare 3 arrays of rows and columns sizes named a,b and sum.

The ‘a’ array is used to store the elements of the first matrix and the ‘b’ array is used to store the elements of the second matrix similarly, the 3rd array ‘sum’ is used to store the result of the addition of the two matrices.

Source Code Of C Program To Add Two Matrices:

#include<stdio.h>
#define rows 3
#define cols 3
int main(){
int a[rows][cols],b[rows][cols],sum[rows][cols];
printf("Enter 3x3 Elements Of First Array : \n");
for(int i=0; i<rows ;i++){
for(int j = 0 ; j <cols; j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter 3x3 Elements Of Second Array : \n");
for(int i=0; i<rows ;i++){
for(int j = 0 ; j <cols; j++){
scanf("%d",&b[i][j]);
}
}
for(int i=0; i<rows ;i++){
for(int j = 0 ; j <cols; j++){
sum[i][j] = a[i][j] + b[i][j];
}
}
for(int i=0; i<rows ;i++){
for(int j = 0 ; j <cols; j++){
printf("%d\t",sum[i][j]);
}
printf("\n");
}
return 0;
}

Output:

C Program To Add Two Matrices
C Program To Add Two Matrices

 

 

 

 

 

 

 

From the above article, we conclude how to write a C Program To Add Two Matrices. We hope that you understand the program and their logic easily.

 

Leave a Reply

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