Site icon DataFlair

C Program to Add Two Matrix

Program 1

#include<stdio.h>
#include<conio.h>
int main()
{
    int a[50][50],b[50][50],result[50][50],r,c,r1,c1,m,n;
    printf("\nEnter Values of row and col for first matrix");
    scanf("%d%d",&m,&n);
    // First Matrix input
    printf("\nEnter elements of first matrix.");
    for(r=0;r<m;r++)
    {
        for(c=0;c<n;c++)
        {
           scanf("%d",&a[r][c]);
        } 
    }
    // Second Matrix input
    printf("\nEnter elements of first matrix.\n");
    for(r=0;r<m;r++)
    {
        for(c=0;c<n;c++)
        {
           scanf("%d",&b[r][c]);
        } 
    }
// First Matrix print
    printf("\nFirst Matrix: \n");
    for(r=0;r<m;r++)
    {
        for(c=0;c<n;c++)
        {
          printf("%5d",a[r][c]);
        } 
        printf("\n");
    }
    // Second Matrix print
    printf("\nSecond Matrix: \n");
    for(r=0;r<m;r++)
    {
        for(c=0;c<n;c++)
        {
           printf("%5d",b[r][c]);
        } 
        printf("\n");
    }
    // Addition of two matrix
   for(r=0;r<m;r++)
   {
      for(c=0;c<n;c++)
      result[r][c]=a[r][c]+b[r][c];
   }
// Result Matrix print
    printf("\nSecond Matrix: \n");
    for(r=0;r<m;r++)
    {
        for(c=0;c<n;c++)
        {
           printf("%5d",result[r][c]);
        } 
        printf("\n");
    }
    return 0;
}

 

Exit mobile version