C

Matrix Multiplication in C

In this tutorial, we are going to see how to multiply two matrices in C. The following program asks the user to enter two matrices and then calculates the multiplication of these two matrices.
 

100-multiple-choice-questions-in-c-programming100 Multiple Choice Questions In C Programming – Part 1This collection of 100 Multiple Choice Questions and Answers (MCQs) In C Programming : Quizzes & Practice Tests with Answer focuses on “C Programming”.  …Read More

 

Matrix Multiplication in C
#include<stdio.h> 
#include<stdlib.h>

int main(){
  int mat1[10][10], mat2[10][10], res[10][10];
  int n,m,i,j,k;

    printf("Enter the number of rows and columns:\n");
    scanf("%d%d",&n,&m);

    printf("Enter the elements of the first matrix\n");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            scanf("%d",&mat1[i][j]);
        }
    }

    printf("Enter the elements of the second matrix\n");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            scanf("%d",&mat2[i][j]);
        }
    } 
    
  printf("Matrix multiplication = \n");
  for(i = 0; i < n; i++)
  {
    for(j = 0; j < m; j++)
    {
      res[i][j]=0;
      for(k = 0; k < m; k++)
      {
        res[i][j] += mat1[i][k] * mat2[k][j];
      }
    }
  }
  //Display result
  for(i = 0; i < n; i++)
  {
    for(j = 0; j < m; j++) 
    {
      printf("%d\t",res[i][j]);
    }
    printf("\n");
  }
  return 0;
}

Output:

Enter the number of rows and columns:
2 2
Enter the elements of the first matrix
1 2
1 3
Enter the elements of the second matrix
3 2
1 1
Matrix multiplication = 
5       4
6       5

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *