My Subject Blog

C Program to do Matrix Addition

/*
Write a program to Add two Matrices. i.e.  C= A + B. (Matrix Addition)
*/

#include<stdio.h>
#define size 5

int main()
    {
    int c[size][size],a[size][size],b[size][size],i,j,m1,n1,m2,n2;
    printf("Enter the order of 1st matrix \n");
    printf("Rows \t");
    scanf("%d",&m1);
    printf("\n Column\t");
    scanf("%d",&n1);
    printf("Enter the order of 2st matrix \n");
    printf("Rows\t");
    scanf("%d",&m2);
    printf("\n Column\t");
    scanf("%d",&n2);

    if(m1!=m2 && n1!=n2)
    {
     printf("\nMatrix Addition is not possible");
    }
    else
    {
      printf("\nEnter the elements of 1st array");
     for(i=0;i<m1;i++)
      {
        for(j=0;j<n1;j++)
         {
          scanf("%d",&a[i][j]);
         }
      }
     
      for(i=0;i<m1;i++)
      {
        for(j=0;j<n1;j++)
         {
          printf("%d\t",a[i][j]);
         }
        printf("\n");
      }
     
    printf("\nEnter the elements of 2nd array");
     for(i=0;i<m2;i++)
      {
        for(j=0;j<n2;j++)
        {
         scanf("%d",&b[i][j]);
         }
      }
     
      for(i=0;i<m2;i++)
      {
         for(j=0;j<n2;j++)
          {
           printf("%d\t",b[i][j]);
           }
       printf("\n");
      }
     
      
     printf("Addition is \n");

    for(i=0;i<m1;i++)
     {
          for(j=0;j<n1;j++)
         {
           c[i][j] = a[i][j] + b[i][j];
           printf("%d\t",c[i][j]);
         }
     }
     
    }
    return 0;
    }

Comments

Most Visited Post

C Program to Find Largest Odd Number from Array

C Program for Runtime Memory Allocation for Table of Integers

Followers