Multiply given two matrix of given order - C

Q. Write a program to multiply given two matrix of given order.
#BIT-C Programming-2019-GroupB-5No-8 Marks-Solution

==> Code goes like this:


#include<stdio.h>
int main()
{
  int row1, col1, row2, col2, i, j, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];

  printf("Enter number of rows and columns of first matrix:\n");
  scanf("%d%d", &row1, &col1);
  printf("Enter elements of first matrix:\n");

  for (i = 0; i < row1; i++)
    for (j = 0; j < col1; j++)
      scanf("%d", &first[i][j]);

  printf("Enter number of rows and columns of second matrix:\n");
  scanf("%d%d", &row2, &col2);

  if (col1 != row2)
    printf("The multiplication isn't possible.\n");
  else
  {
    printf("Enter elements of second matrix:\n");

    for (i = 0; i < row2; i++)
      for (j = 0; j < col2; j++)
        scanf("%d", &second[i][j]);

    for (i = 0; i < row1; i++) {
      for (j = 0; j < col2; j++) {
        for (k = 0; k < row2; k++) {
          sum = sum + first[i][k]*second[k][j];
        }

        multiply[i][j] = sum;
        sum = 0;
      }
    }

    printf("Product of the matrices:\n");

    for (i = 0; i < row1; i++) {
      for (j = 0; j < col2; j++)
        printf("%d\t", multiply[i][j]);

      printf("\n");
    }
  }

  return 0;
}

Output:



















If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment