C program for Matrix Addition.


#include<stdio.h>
#include<conio.h>
int main()
{
    int a[50][50],b[50][50],c[50][50];
    int i,j,m,n,k,l;
    clrscr();
    printf("Enter rows of matrix a:");
    scanf("%d",&m);
    printf("Enter columns of matrix a:");
    scanf("%d",&n);
    printf("Enter rows of matrix b:");
    scanf("%d",&k);
    printf("Enter columns of matrix b:");
    scanf("%d",&l);
     if((m != k) && (n != l))
    {
     printf("**Sorry,Matrix addition is not possible here,\nBecause order of Matrices are different.**");
     exit(0);
    }
    printf("**Matrix addition is possible**");
    printf("\nEnter elements of matrix a:\n");
    for(i=0;i<m;i++)
    {
      for(j=0;j<n;j++)
     {
       scanf("%d",&a[i][j]);
     }
    }
    printf("Enter elements of matrix b:\n");
     for(i=0;i<k;i++)
    {
        for(j=0;j<l;j++)
        {
            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("Addition of Matrix is:\n");
    for(i=0;i<m;i++)
    {
     for(j=0;j<n;j++)
     {
      printf("%d  ",c[i][j]);
    }
     printf("\n");
    }
     return 0;
}

*** Input && Output ***





Post a Comment

0 Comments