C program to multiply two matrices. Multiplication of two matrixmces in c. Multiplication of matrices in c programming | My CS Tutorial
In this tutorial we will learn or discuss that how to write a c program code to multiply two matrix. In this program we will use arrays.
This c program code will multiply two matrices and find a new matrix.
In the first user will enter range of rows and columns of both matrices. After it user enter the elements of first matrix then second matrix.
After it compiler check and exicute the condition and logics and print a new matrix in which both matrixes are multiplied.
C PROGRAM CODE
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,rows_1,col_1,rows_2,col_2,sum=0;
int a1[100][100],a2[100][100],mul[100][100];
clrscr();
printf("\n ENTER THE RANGE OF ROWS AND COLUMNS OF FIRST MATRIX:\n ");
scanf("%d %d",&rows_1,&col_1);
printf("\n ENTER THE RANGE OF ROWS AND COLUMNS OF SECOND MATRIX:\n ");
scanf("%d %d",&rows_2,&col_2);
if(col_1==rows_2)
{
//Taking input for 1st matrix
printf("\n ENTER THE NUMBERS OF FIRST MATRIX:\n ");
for(i=0;i<rows_1;i++)
{
for(j=0;j<col_1;j++)
{
scanf("%d",&a1[i][j]);
}
}
//Taking input for 2nd matrix
printf("\n ENTER THE NUMBERS OF SECOND MATRIX:\n ");
for(i=0;i<rows_2;i++)
{
for(j=0;j<col_2;j++)
{
scanf("%d",&a2[i][j]);
}
}
//multiplication of matrix
for(i=0;i<rows_1;i++)
{
for(j=0;j<col_2;j++)
{
for(k=0;k<rows_2;k++)
{
sum+=a1[i][k]*a2[k][j];
}
mul[i][j]=sum;
sum=0;
}
}
printf("\n MULTIPLICATION OF ABOVE MATRICES IS:\n ");
for(i=0;i<rows_1;i++)
{
for(j=0;j<col_2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n ");
}
}
else
{
printf("\n NOT POSSIBLE WITH THE GIVEN ROWS AND COLUMNS ELEMENTS....\n ");
}
getch();
}
OUTPUT:
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment