Matrix Program #2

9. Read two matrices, first m x n and second n x t, perform multiplication operation and store result in third matrix and print it.

#include<stdio.h>
void main()
{
   int i, j,k, mat1[10][10], mat2[10][10], mat3[10][10];
   int row1, col1, row2, col2,sum=0;

   printf("Enter the number of Rows of Mat1 : ");
   scanf("%d", &row1);
   printf("Enter the number of Columns of Mat1 : ");
   scanf("%d", &col1);

   printf("Enter the number of Rows of Mat2 : ");
   scanf("%d", &row2);
   printf("Enter the number of Columns of Mat2 : ");
   scanf("%d", &col2);

   /* Before accepting the Elements Check if no of
     columns of matrix 1 and rows of martix 2 is equal */
    if(col1!=row2)
     {
          printf("\nOrder of two matrices is not same\nMatrix is not multiplied\n");
          return ;
     }
  printf("\n");

   //Accept the Elements in Matrix 1
   for (i = 0; i < row1; i++)
   {
      for (j = 0; j < col1; j++)
      {
         printf("Enter the Element a[%d][%d] : ", i, j);
         scanf("%d", &mat1[i][j]);
      }
   }
   printf("\n");

   //Accept the Elements in Matrix 2
   for (i = 0; i < row2; i++)
   {
      for (j = 0; j < col2; j++)
      {
         printf("Enter the Element b[%d][%d] : ", i, j);
         scanf("%d", &mat2[i][j]);
      }
    } 

   //Print out the Matrix 1
   printf("\nThe Matrix 1 is : \n");
   for (i = 0; i < row1; i++)
   {
      for (j = 0; j < col1; j++)
      {
         printf("%d\t", mat1[i][j]);
      }
      printf("\n");
   }

  //Print out the Matrix 2
   printf("\nThe Matrix 2 is : \n");
   for (i = 0; i < row2; i++)
   {
      for (j = 0; j < col2; j++)
      {
         printf("%d\t", mat2[i][j]);
      }
      printf("\n");
   }
  
   //Multiplication Logic
   for (i = 0; i < row1; i++)
   {
      for (j = 0; j < col2; j++)
      {
         sum = 0;
         for (k = 0; k < row2; k++)
         {
            sum = sum + mat1[i][k] * mat2[k][j];
         }
         mat3[i][j] = sum;
      }
   }

   printf("\nMultiplication Of Two Matrices : \n");
   for (i = 0; i < row1; i++)
   {
      for (j = 0; j < col2; j++)
      {
         printf(" %d ", mat3[i][j]);
      }
      printf("\n");
   }
}

 Output

  • Enter the number of Rows of Mat1 : 2
    Enter the number of Columns of Mat1 : 2
    Enter the number of Rows of Mat2 : 2
    Enter the number of Columns of Mat2 : 2

    Enter the Element a[0][0] : 1
    Enter the Element a[0][1] : 2
    Enter the Element a[1][0] : 1
    Enter the Element a[1][1] : 2

    Enter the Element b[0][0] : 1
    Enter the Element b[0][1] : 2
    Enter the Element b[1][0] : 1
    Enter the Element b[1][1] : 2

    The Matrix 1 is :
    1    2   
    1    2   

    The Matrix 2 is :
    1    2   
    1    2   

    Multiplication Of Two Matrices :
     3  6
     3  6
  • Enter the number of Rows of Mat1 : 2
    Enter the number of Columns of Mat1 : 3
    Enter the number of Rows of Mat2 : 2
    Enter the number of Columns of Mat2 : 3

    Order of two matrices is not same
    Matrix is not multiplied



Note:

To multiply two matrices, the number of columns of first matrix should be equal to the number of rows to second matrix.

This program displays the error until the number of columns of first matrix is equal to the number of rows of second matrix.

For download this program please check the link ➡➡ Download

Comments

Popular

Basic Program #3

Pointer Program #1

Array Program #4

Singly Linked List program #1

Basic program #2