Sathvikks8

Matrix Multiplication

Apr 24th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int i,j,r1,r2,c1,c2,k,a[100][100],b[100][100],c[100][100];
  5.     goto input;
  6.     input:
  7.     {
  8.         printf("Enter the number of rows and columns of matrix A: ");
  9.         scanf("%d%d",&r1,&c1);
  10.         printf("\nEnter the number of rows and columns of matrix B: ");
  11.         scanf("%d%d",&r2,&c2);
  12.     }
  13.     if(c1!=r2)
  14.     {
  15.         printf("\nThe matrices cannot be multiplied. Try again\n\n");
  16.         goto input;
  17.     }
  18.     printf("\nEnter the elements of matrix A: ");
  19.     for(i=0;i<r1;i++)
  20.     {
  21.         for(j=0;j<c1;j++)
  22.             scanf("%d",&a[i][j]);
  23.     }
  24.     printf("\nEnter the elements of matrix B: ");
  25.     for(i=0;i<r2;i++)
  26.     {
  27.         for(j=0;j<c2;j++)
  28.             scanf("%d",&b[i][j]);
  29.     }
  30.     for(i=0;i<r1;i++)
  31.     {
  32.         for(j=0;j<c2;j++)
  33.         {
  34.             c[i][j]=0;
  35.             for(k=0;k<c1;k++)
  36.                 c[i][j]+=a[i][k]*b[k][j];
  37.         }
  38.     }
  39.     printf("\nThe entered matrix A is\n");
  40.     for(i=0;i<r1;i++)
  41.     {
  42.         for(j=0;j<c1;j++)
  43.         {
  44.             printf("%d\t",a[i][j]);
  45.         }
  46.         printf("\n");
  47.     }
  48.     printf("\nThe entered matrix B is\n");
  49.     for(i=0;i<r2;i++)
  50.     {
  51.         for(j=0;j<c2;j++)
  52.         {
  53.             printf("%d\t",b[i][j]);
  54.         }
  55.         printf("\n");
  56.     }
  57.     printf("\nThe product of the matrices A & B i.e C is\n");
  58.     for(i=0;i<r1;i++)
  59.     {
  60.         for(j=0;j<c2;j++)
  61.         {
  62.             printf("%d\t",c[i][j]);
  63.         }
  64.         printf("\n");
  65.     }
  66. }
Add Comment
Please, Sign In to add comment