Let Us C (Chapter 8 : Arrays) : Program-13_2
(2)Write a program to multiply any two 3 x 3 matrices.
// Let Us C (Chapter 8 : Arrays) : Program-13_2
/* (2)Write a program to multiply any two 3 x 3 matrices. */
#include<stdio.h>
#include<conio.h>
void main()
{
int M1[3][3], M2[3][3], M3[3][3], i,j,k,sum;
clrscr();
printf("\nProgram for Multiplication of Matrices");
printf("\nEnter two 3 X 3 matrices");
printf("\nEnter 9 elements of 1st Matrix separated by space\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
//printf(" Enter Element %d : ",M1[i][j]);
scanf("%d",&M1[i][j]);
}
}
printf("\nEnter 9 elements of 2nd Matrix separated by space\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
//printf(" Enter Element %d : ",M2[i][j]);
scanf("%d",&M2[i][j]);
}
}
printf("***** 1st Matrix *****\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%4d",M1[i][j]);
}
printf("\n");
}
printf("***** 2nd Matrix *****\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%4d",M2[i][j]);
}
printf("\n");
}
printf("***** Multiplication of Matrices *****\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum = 0;
for(k=0;k<3;k++)
{
sum = sum + M1[i][k]*M2[k][j];
}
M3[i][j] = sum;
printf("%4d",M3[i][j]);
}
printf("\n");
}
}
OUTPUT
Comments
Post a Comment