C Program to multiply two matrices
#include<stdio.h>
#include<stdlib.h>
int main()
{
int rows1, columns1, rows2, columns2, matrix1[50][50],matrix2[50][50],matrix3[50][50],sum=0;
printf("------------------Programme to find the multiplication of a matrix:----------------------\n");
printf("Enter no. of rows of first matrix:\n");
scanf("%d",&rows1);
printf("Enter no. of columns of first matrix:\n");
scanf("%d",&columns1);
printf("Enter the element of first matrix:\n");
int i,j;
for(i=0; i<rows1; i++)
{
for(j=0; j<columns1; j++)
{
printf("Element::[%d,%d]",i,j);
scanf("%d",&matrix1[i][j]);
}
}
printf("Enter no. of rows of second matrix:\n");
scanf("%d",&rows2);
printf("Enter no. of columns of second matrix:\n");
scanf("%d",&columns2);
if(rows1==columns2)
{
printf("Enter the element of second matrix:\n");
for(i=0; i<rows2; i++)
{
for(j=0; j<columns2; j++)
{
printf("Element::[%d,%d]",i,j);
scanf("%d",&matrix2[i][j]);
}
}
}
else
{
printf("Please enter a matrix of equal order:\n");
}
int m,n,o;
for ( m= 0 ; m< rows1; m++ )
{
for ( n= 0 ; n< columns2 ; n++ )
{
for ( o = 0 ; o <rows2;o++ )
{
sum = sum + matrix1[m][o]*matrix2[o][n];
}
matrix3[m][n] =sum;
sum = 0;
}
}
printf("Answer:\n");
for(i=0;i<rows1;i++)
{
for(j=0;j<columns2;j++)
{
printf("[%3d] ",matrix3[i][j]);
}
printf("\n");
}
return 0;
OUTPUT:
}
Comments
Post a Comment