C program to add two matrices

Below is the program in the C programming language to add two matrices and store the result in the third matrix –

/* C program to perform addition on two matrices */
#include<stdio.h>
#include<conio.h>
void main()
{
     int i,j,ar1[100][100],ar2[100][100],ar3[100][100],m,n;
     
     printf("Enter values of m and n to set order m x n of both the matrices");
     scanf("%d %d",&n,&m);
     
     printf("Enter elements of 1st matrix");
     for(i=0;i<n;i++)
     {
           for(j=0;j<m;j++)
           {
                 scanf("%d",&ar1[i][j]);
           }
     }
     
     printf("Enter elements of 2nd matrix");
     for(i=0;i<n;i++)
     {
           for(j=0;j<m;j++)
           {
                 scanf("%d",&ar2[i][j]);
           }
     }
     
     printf("Matrix after addition of two matrices is -- \n");
     for(i=0;i<n;i++)
     {
           for(j=0;j<m;j++)
           {
                 ar3[i][j]=ar1[i][j]+ar2[i][j];
                 printf("%d\t",ar3[i][j]);
           }
           printf("\n");
     }
     getch();
}

yashan has written 69 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>