Advertisement
ZinedinZidan

untittled

Oct 20th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int first[10][10],second[10][10],result[10][10],c1,c2,r1,r2,i,j,k,sum=0;
  5.  
  6.  
  7.     printf("Enter rows and cols for first matrix : ");
  8.     scanf("%d%d",&r1,&c1);
  9.     printf("Enter rows and cols for second matrix : ");
  10.     scanf("%d%d",&r2,&c2);
  11.  
  12.     while(c1!=r2)
  13.     {
  14.         printf("Error!! col of first matrix not equal to row of second");
  15.         printf("Enter rows and cols for first matrix : ");
  16.         scanf("%d%d",&r1,&c1);
  17.         printf("Enter rows and cols for second matrix : ");
  18.         scanf("%d%d",&r2,&c2);
  19.     }
  20.  
  21.     printf("Enter elements for first matrix: \n");
  22.     for(i=0; i<r1; i++)
  23.     {
  24.         for(j=0; j<c1; j++)
  25.         {
  26.             printf("first[%d][%d] = ",i,j);
  27.             scanf("%d",&first[i][j]);
  28.         }
  29.  
  30.     }
  31.  
  32.  
  33.     printf("\nEnter elements for second matrix: \n");
  34.     for(i=0; i<r2; i++)
  35.     {
  36.         for(j=0; j<c2; j++)
  37.         {
  38.             printf("second[%d][%d] = ",i,j);
  39.             scanf("%d",&second[i][j]);
  40.         }
  41.  
  42.     }
  43.     //multipying matrix
  44.  
  45.     for(i=0; i<r1; i++)
  46.     {
  47.  
  48.         for(j=0; j<c2; j++)
  49.         {
  50.             for(k=0; k<c1; k++)
  51.             {
  52.                 sum=sum+first[i][k]*second[k][j];
  53.             }
  54.             result[i][j]=sum;
  55.             sum=0;
  56.         }
  57.  
  58.     }
  59.     printf("\n\nfirst matrix\n");
  60.     for(i=0; i<r1; i++)
  61.     {
  62.         printf("\t");
  63.         for(j=0; j<c1; j++)
  64.         {
  65.  
  66.             printf("%d ",first[i][j]);
  67.  
  68.         }
  69.         printf("\n");
  70.     }
  71.  
  72.  
  73.     printf("\n\nsecond matrix\n");
  74.     for(i=0; i<r2; i++)
  75.     {
  76.         printf("\t");
  77.         for(j=0; j<c2; j++)
  78.         {
  79.  
  80.             printf("%d ",second[i][j]);
  81.  
  82.         }
  83.         printf("\n");
  84.     }
  85.  
  86.  
  87.     printf("\n\nResult Matrix\n");
  88.     for(i=0; i<r1; i++)
  89.     {
  90.         printf("\t");
  91.         for(j=0; j<c2; j++)
  92.          printf("%d ",result[i][j]);
  93.             printf("\n");
  94.  
  95.     }
  96.  
  97.  
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement