C Programming Tutorial

Advertisements

Write a program to display product of two matrices in C



       #include<stdio.h>
       #include<conio.h>

       void main()
       {

              const int s=3;
              int arr1[s][s],arr2[s][s],pro[s][s],i,j,sum=0,k;
              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]);
                     }
           }
              
              for( k=0;k<<s;k++)
              {
              printf("\n");

              for(i=0;i<<s;i++)
              {
                     sum=0;

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

              getch();
    }

   Output : 

              Enter Values for Matrix 1
              Enter any number :  1
              Enter any number :  2
              Enter any number :  3
              Enter any number :  4
              Enter any number :  5
              Enter any number :  6
              Enter any number :  7
              Enter any number :  8
              Enter any number :  9
              
              Enter Values for Matrix 2
              Enter Values for Matrix 1
              Enter any number :  1
              Enter any number :  2
              Enter any number :  3
              Enter any number :  4
              Enter any number :  5
              Enter any number :  6
              Enter any number :  7
              Enter any number :  8
              Enter any number :  9

              Values in Matrix 1
                     1  2  3
                     4  5  6
                     7  8  9

              Values in Matrix 2
                     1  2  3
                     4  5  6
                     7  8  9

              Product of Matrix 1 and 2
                     30      36     42
                     66      81     96
                     102    126    105




Advertisement