w3resource

C Exercises: Multiplication of two Matrices

C Array: Exercise-21 with Solution

Write a program in C for the multiplication of two square matrices.

Pictorial Presentation:

C Exercises: Multiplication of two Matrices

Sample Solution:

C Code:

#include <stdio.h>

void main()
{
  int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;
  
       printf("\n\nMultiplication of two Matrices :\n");
       printf("----------------------------------\n");  
  
  printf("\nInput the rows and columns of first matrix : ");
  scanf("%d %d",&r1,&c1);
  printf("\nInput the rows and columns of second matrix : ");
  scanf("%d %d",&r2,&c2);
  if(c1!=r2){
      printf("Mutiplication of Matrix is not possible.");
      printf("\nColumn of first matrix and row of second matrix must be same.");
  }
  else
      {
       printf("Input elements in the first matrix :\n");
       for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
            {
	           printf("element - [%d],[%d] : ",i,j);
	           scanf("%d",&arr1[i][j]);
            }
        }   
       printf("Input elements in the second matrix :\n");
       for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
            {
	           printf("element - [%d],[%d] : ",i,j);
	           scanf("%d",&brr1[i][j]);
            }
        }    
 	 printf("\nThe First matrix is :\n");
  		for(i=0;i<r1;i++)
    		{
      		printf("\n");
      		for(j=0;j<c1;j++)
          	printf("%d\t",arr1[i][j]);
    		}
  
  	printf("\nThe Second matrix is :\n");
  		for(i=0;i<r2;i++)
    		{
      		printf("\n");
      		for(j=0;j<c2;j++)
      		printf("%d\t",brr1[i][j]);
    		}
//multiplication of matrix
      for(i=0;i<r1;i++)
          for(j=0;j<c2;j++)
           crr1[i][j]=0;
             for(i=0;i<r1;i++)    //row of first matrix
                 { 
                   for(j=0;j<c2;j++)    //column of second matrix
                     {  
                       sum=0;
                         for(k=0;k<c1;k++)
                           sum=sum+arr1[i][k]*brr1[k][j];
                           crr1[i][j]=sum;
                     }
                 }
  printf("\nThe multiplication of two matrices is : \n");
  for(i=0;i<r1;i++)
     {
        printf("\n");
        for(j=0;j<c2;j++)
         {
           printf("%d\t",crr1[i][j]);
         }
     }
  }
printf("\n\n");
}

Sample Output:

Multiplication of two Matrices :                                                                              
----------------------------------                                                                            
                                                                                                              
Input the rows and columns of first matrix : 2                                                                
2                                                                                                             
                                                                                                              
Input the rows and columns of second matrix : 2                                                               
2                                                                                                             
Input elements in the first matrix :                                                                          
element - [0],[0] : 1                                                                                         
element - [0],[1] : 2                                                                                         
element - [1],[0] : 3                                                                                         
element - [1],[1] : 4                                                                                         
Input elements in the second matrix :                                                                         
element - [0],[0] : 5                                                                                         
element - [0],[1] : 6                                                                                         
element - [1],[0] : 7                                                                                         
element - [1],[1] : 8 

The First matrix is :                                                                                         
                                                                                                              
1       2                                                                                                     
3       4                                                                                                     
The Second matrix is :                                                                                        
                                                                                                              
5       6                                                                                                     
7       8                                                                                                     
The multiplication of two matrices is :                                                                       
                                                                                                              
19      22                                                                                                    
43      50                                          

Flowchart:

Flowchart: Multiplication of two Matrices

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C for subtraction of two Matrices.
Next: Write a program in C to find transpose of a given matrix.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP