Friday, 16 March 2018

Addition of two matrix in C

In C Programming addition of matrix is all a very significant program while we are learning array in C.
So, here we gonna do Addition of matrix.

/*Pre-Processors */
#include<stdio.h>
#include<conio.h>
#define MAX 15 /* Length MAX*/
int main()
{
   int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX];
   int i, j;
   int m,n;
   clrscr();
   printf("Enter no. of rows: ");
   scanf("%d",&m);
   printf("Enter no. of columns: ");
   scanf("%d",&n);

   printf("Enter elements of 1st matrix\n");
   for(i=0; i<m; ++i)
    for(j=0; j<n; ++j)
    {
       printf("Enter a%d%d: ", i+1, j+1);
       scanf("%d", &a[i][j]);
    }
   printf("Enter elements of 2nd matrix\n");
   for(i=0; i<m; ++i)
    for(j=0; j<n; ++j)
    {
       printf("Enter b%d%d: ", i+1, j+1);
       scanf("%d", &b[i][j]);
    }
   for(i=0; i<m; ++i)
    for(j=0; j<n; ++j)
    {
       c[i][j] = a[i][j] + b[i][j];
    }
   printf("\nSum Of Matrix:\n");

   for(i=0; i<m; ++i)
    for(j=0; j<n; ++j)
    {
       printf("%2d", c[i][j]);

       if(j==1)
          printf("\n");
    }
    getch();
return 0;
}

No comments:

Post a Comment