To add two matrices in C programming, you have to ask to the
user to enter the elements of both the matrix, now start adding the two matrix
to form a new matrix. After adding two matrices display the third matrix which
is the result of the addition of the two matrices.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int matrix1[3][3], matrix2[3][3], i, j, matrix3[3][3];
printf("Enter matrix 1 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&matrix1[i][j]);
}
}
printf("Enter matrix 2 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&matrix2[i][j]);
}
}
printf("Adding the two matrix to form the third matrix .....\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
matrix3[i][j]=matrix1[i][j]+matrix2[i][j];
}
}
printf("The two matrix added successfully...!!");
printf("The new matrix will be :\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",matrix3[i][j]);
}
printf("\n");
}
getch();
}
#include<conio.h>
void main()
{
clrscr();
int matrix1[3][3], matrix2[3][3], i, j, matrix3[3][3];
printf("Enter matrix 1 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&matrix1[i][j]);
}
}
printf("Enter matrix 2 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&matrix2[i][j]);
}
}
printf("Adding the two matrix to form the third matrix .....\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
matrix3[i][j]=matrix1[i][j]+matrix2[i][j];
}
}
printf("The two matrix added successfully...!!");
printf("The new matrix will be :\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",matrix3[i][j]);
}
printf("\n");
}
getch();
}
Run:
Previous Next
No comments:
Post a Comment