Table of Contents
C program to add two matrices
C program to add two matrices : matrix is a collection of elements stored in rows and column format.
Before adding matrices we have to check if dimensions for both the matrix is same.
Then we will add two numbers from both the matrices which has same positions in their respective matrix.
Program for matric addition in C
#include<stdio.h>
void disp_matrix(int d[3][3])
{
int n,m;
for(n=0; n<3; n++)
{
for(m=0; m<3; m++)
{
printf("%d t",d[n][m]);
}
printf("n");
}
printf("n");
}
//itvoyagers.in
int main()
{
int i, j, x[3][3], y[3][3], rsl[3][3];
printf("Enter any values for first matrix : n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("Matrix_1[%d][%d] : ",i,j);
scanf("%d",&x[i][j]);
}
}
//itvoyagers.in
printf("Enter any values for second matrix : n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("Matrix_2[%d][%d] : ",i,j);
scanf("%d",&y[i][j]);
}
}
//itvoyagers.in
printf("Resultant matrix n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
rsl[i][j] = x[i][j] + y[i][j];
}
}
//itvoyagers.in
printf("First Matrixn");
disp_matrix(x);
printf("Second Matrixn");
disp_matrix(y);
printf("Resultant Matrixn");
disp_matrix(rsl);
return 0;
}
Output :
Enter any values for first matrix :
Matrix_1[0][0] : 1
Matrix_1[0][1] : 5
Matrix_1[0][2] : 4
Matrix_1[1][0] : 8
Matrix_1[1][1] : 6
Matrix_1[1][2] : 3
Matrix_1[2][0] : 7
Matrix_1[2][1] : 2
Matrix_1[2][2] : 5
Enter any values for second matrix :
Matrix_2[0][0] : 9
Matrix_2[0][1] : 5
Matrix_2[0][2] : 3
Matrix_2[1][0] : 4
Matrix_2[1][1] : 2
Matrix_2[1][2] : 6
Matrix_2[2][0] : 8
Matrix_2[2][1] : 1
Matrix_2[2][2] : 3
Resultant matrix
First Matrix
1 5 4
8 6 3
7 2 5
Second Matrix
9 5 3
4 2 6
8 1 3
Resultant Matrix
10 10 7
12 8 9
15 3 8
PRACTICALS/PRACTICE PROGRAM IN C
CHECKOUT OTHER RELATED TOPICS
CHECKOUT OTHER QUIZZES
Quizzes on Operator |
Easy Quiz on Operators in C part 1 |
Easy quiz on operators in C part 2 |
Easy quiz on operators in C part 3 |
Quizzes on Loops |
Easy quiz on loops in C part 1 |
Easy quiz on loops in C part 2 |
Easy quiz on loops in C part 3 |
Quizzes on Decision Making Statement |
Easy Quiz On Decision Making Statements Part 1 |
Easy Quiz On Decision Making Statements Part 2 |
Quizzes on String |
Easy quiz on String in C programming part 1 |
Quizzes on Array |
Easy quiz on Array in C programming – part 1 |
5 – Best Java program to convert decimal to binary |
New Quizzes Coming soon.. |
