addition and subtraction of two matrices.c

#include <stdio.h>

 

int main()

{

   int m, n, c, d, first[10][10], second[10][10], sum[10][10];

 

   printf("Enter the number of rows and columns of matrix\n");

   scanf("%d%d", &m, &n);

   printf("Enter the elements of first matrix\n");

 

   for ( c = 0 ; c < m ; c++ )

      for ( d = 0 ; d < n ; d++ )

         scanf("%d", &first[c][d]);

 

   printf("Enter the elements of second matrix\n");

 

   for ( c = 0 ; c < m ; c++ )

      for ( d = 0 ; d < n ; d++ )

            scanf("%d", &second[c][d]);

 

   for ( c = 0 ; c < m ; c++ )

      for ( d = 0 ; d < n ; d++ )

         sum[c][d] = first[c][d] + second[c][d];

 

   printf("Sum of entered matrices:-\n");

 

   for ( c = 0 ; c < m ; c++ )

   {

      for ( d = 0 ; d < n ; d++ )

         printf("%d\t", sum[c][d]);

 

      printf("\n");

   }

    printf("\n");

 

   for ( c = 0 ; c < m ; c++ )

      for ( d = 0 ; d < n ; d++ )

         sum[c][d] = first[c][d] - second[c][d];

 

    printf("Difference between entered matrices:-\n");

    for ( c = 0 ; c < m ; c++ )

   {

      for ( d = 0 ; d < n ; d++ )

         printf("%d\t", sum[c][d]);

 

      printf("\n");

   }

   return 0;

}

OUTPUT

Enter the number of rows and columns of matrix
3 3
Enter the elements of first matrix
1 2 0
0 1 1
2 0 1
Enter the elements of second matrix
1 1 2
2 1 1
1 2 1
Sum of entered matrices:-
2       3       2
2       2       2
3       2       2

Difference between entered matrices:-
0       1       -2
-2      0       0
1       -2      0
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s