Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

C program for Matrix Multiplication Tutorial

C program for Matrix Multiplication Tutorial

In this tutorial I am going to show you how to multiply Matrices using C language. We take input from keyboard for  two Matrices and multiply them to show the output.

Environment –  Linux 64bit (Linux Mint 18.1) , IDE – Geany , Compiler – GCC

Screen shot of Output :

C program for Matrix Multiplication Code


#include <stdio.h>

void get_matrix(int[][10],int,int,int);
void put_matrix(int[][10],int,int);

int main()
{
int mat1[10][10],mat2[10][10],r,c,r1,mat3[10][10],row,col,col2,sum;
printf("\n Matrix Multiplication \n");
printf("\nEnter the number of rows and columns for the matrix1 : ");
scanf("%d%d",&row,&col);
printf("\nEnter the elements of matrix 1 : (Row-wise)\n");
get_matrix(mat1,row,col,1);
printf("\nInput %d X %d Matrix 1 : \n",row,col);
put_matrix(mat1,row,col);
printf("\nEnter the number of columns for the matrix 2 : \n");
scanf("%d",&col2);
printf("\nEnter the elements of matrix 2 : (Row-wise)\n");
get_matrix(mat2,col,col2,2);
printf("\nInput %d X %d Matrix 2 : \n",col,col2);
put_matrix(mat2,col,col2);
sum=0;
for(r=0;r<row;r++)
{
for(c=0;c<col2;c++)
{
for(r1=0;r1<col;r1++)
{
sum=sum+mat1[r][r1]*mat2[r1][c];
}
mat3[r][c]=sum;
sum=0;
}
}
printf("\nResultant %d X %d Matrix\n",row,col2);
put_matrix(mat3,row,col2);
return 0;
}

void get_matrix(int matrix[][10],int row,int col,int n)
{
int i,j;
for(i=0;i<row;i++)
{
printf("\n Row %d",i+1);
for(j=0;j<col;j++)
{
printf("\n matrix%d[%d][%d] = ",n,i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
}

void put_matrix(int matrix[][10],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d",matrix[i][j]);
printf("\t");
}
printf("\n");
}
}

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments