Friday, February 15, 2019

Multiplication of two Matrices in C - Language Using Ubuntu

File Name : mulmat.c

#include <stdio.h>
void main()
{
 int m,n,p,q,c,d,k,sum=0;
 int f[10][10],s[10][10],mul[10][10];
 printf("Enter the number of rows and columns of First Matrix\n");
 scanf("%d%d",&m,&n);
 printf("Enter the elements of First Matrix\n");
 for(c=0;c<m;c++)
    for(d=0;d<n;d++)
       scanf("%d",&f[c][d]);
 printf("Enter the number of rows and columns of Second Matrix\n");
 scanf("%d%d",&p,&q);
 if(n!=p)
    printf("The matrices multiplication is not possible.\n");
 else
 {
  printf("Enter the elements of Second Matrix\n");
  for(c=0;c<p;c++)
    for(d=0;d<q;d++)
        scanf("%d",&s[c][d]);    
  for(c=0;c<m;c++)
     {
      for(d=0;d<q;d++)
         {
          for(k=0;k<p;k++)
             {
               sum=sum+f[c][k]*s[k][d];
             }
          mul[c][d]=sum;
          sum = 0;
         }
     }
 printf("Product of Matrices is:\n");
 for(c=0;c<m;c++)
    {  
     for(d=0;d<q;d++)
         printf("%d\t", mul[c][d]);
         printf("\n");
    }
 }
}

Output 1:
Enter the number of rows and columns of First Matrix
2
2
Enter the elements of First Matrix
1
2
3
4
Enter the number of rows and columns of Second Matrix
2
2
Enter the elements of Second Matrix
1
2
3
4
Product of Matrices is:
7    10   
15    22

Output 2:
Enter the number of rows and columns of First Matrix
2
2
Enter the elements of First Matrix
1
2
3
4
Enter the number of rows and columns of Second Matrix
4
5
The matrices multiplication is not possible.

3 comments:

Hadoop Commands

HADOOP COMMANDS OS : Ubuntu Environment Author : Bottu Gurunadha Rao Created: 31-Jan-2022 Updated: 31-Jan-2022 Release  : 1.0.1 Purpose: To ...

Search This Blog