Blogger templates

Pages

Tuesday, 3 July 2012

Write a C Program to Print the Sum of Two Matrices of Same Order

Sum of Matrices

Statement of C Program: This Program accepts two Matrices and Find the Sum of these Matrices and prints the Sum Matrix.

Condition: Both the Matrices are of Same Order i.e Same Order Matrices can be Added or Substracted.

#include<stdio.h>
#include<conio.h>
void main()
{
int Matrix1 [2][2] , Matrix2 [2][2] , Matrixsum[2][2] ;             
int i, j;                                        /* 'i' used for rows and 'j' used for columns */
clrscr();

printf(" Enter the elements of Matrix1\n");
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
scanf("%d" , &Matrix1[i][j] );
}
}                        /* Elements of Matrix Read (Input) with the help of scanf() Function */


printf(" Enter the elements of Matrix2\n");
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
scanf("%d" , &Matrix2[i][j] );
}
}

                                                                   / * Sum of two Matrices  */
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
Matrixsum[i][j] = Matrix1[i][j] + Matrix2[i][j] ;
}
}

printf(" Sum Matrix \n");
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
printf("%d" , Matrixsum[i][j] );
}
printf("\n");
}

getch();
}                                                      /* End of main */

Output:
Enter the elements of Matrix1
3
8
4
6
Enter the elements of Matrix2 
4
0
1
-9
Sum Matrix
7     5
8    -3

                                                                   That's All 

0 comments:

Post a Comment