C Programming Tutorial

Advertisements

Write a program to display sum of two matrices in C



       #include<stdio.h>
       #include<conio.h>
       void main()
       {

              const int s=3;
              int arr1[s][s],arr2[s][s],sum[s][s],i,j;
              clrscr();

              printf("\nEnter Values for Matrix 1 \n");
              for(i=0;i<s;i++)
              {
                     for(j=0;j<s;j++)
                     {
                            printf("\n\tEnter any number : ");
                            scanf("%d",&arr1[i][j]);
                     }
           }

              printf("\nEnter Values for Matrix 2 \n");
              for(i=0;i<s;i++)
              {
                     for(j=0;j<s;j++)
                     {
                            printf("\n\tEnter any number : ");
                            scanf("%d",&arr2[i][j]);
                     }
           }

              printf("\nValues in Matrix 1 \n");
              for(i=0;i<s;i++)
              {
                     printf("\n");
                     for(j=0;j<s;j++)
                     {
                            printf("\t%d",arr1[i][j]);
                     }
           }

              printf("\nValues in Matrix 2 \n");
              for(i=0;i<s;i++)
              {
                     printf("\n");
                     for(j=0;j<s;j++)
                     {
                            printf("\t%d",arr2[i][j]);
                     }
           }

              printf("\nSum of Matrix 1 and 2 \n");
              for(i=0;i<s;i++)
              {
                     printf("\n");
                     for(j=0;j<s;j++)
                     {
                            sum[i][j] = arr1[i][j] + arr2[i][j];
                            printf("\t%d",sum[i][j]);
                     }
           }

              getch();
    }

   Output : 

              Enter Values for Matrix 1
              Enter any number :  5
              Enter any number :  5
              Enter any number :  5
              Enter any number :  5
              Enter any number :  5
              Enter any number :  5
              Enter any number :  5
              Enter any number :  5
              Enter any number :  5

              Enter Values for Matrix 2
              Enter any number :  3
              Enter any number :  3
              Enter any number :  3
              Enter any number :  3
              Enter any number :  3
              Enter any number :  3
              Enter any number :  3
              Enter any number :  3
              Enter any number :  3

              Values in Matrix 1
                     5    5     5
                     5    5     5
                     5    5     5                     

              Values in Matrix 2
                     5    5     5
                     5    5     5
                     5    5     5                     

              Sum of Matrix 1 and 2
                     8    8     8
                     8    8     8
                     8    8     8




Advertisement