//Write a Program in C to Perform Addition on Two 2*2 Matrix and Print it
#include<stdio.h>#include<conio.h>
void main()
{
int i,j,m1[2][2],m2[2][2],m_add[2][2];
clrscr();
printf("Enter four numbers for 1st 2*2 matrix\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&m1[i][j]); //accepting 4 numbers
}
}
printf("\nEnter four numbers for 2nd 2*2 matrix\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&m2[i][j]); //accepting 4 numbers
}
}
printf("\nPrinting 1st Matrix\n");
for(i=0;i<=1;i++)
{
printf("[ ");
for(j=0;j<=1;j++)
{
printf("%d ",m1[i][j]);
}
printf("]\n");
}
printf("\nPrinting 2nd matrix\n");
for(i=0;i<=1;i++)
{
printf("[ ");
for(j=0;j<=1;j++)
{
printf("%d ",m2[i][j]);
}
printf("]\n");
}
//Adding Two Matrix
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
m_add[i][j] = m1[i][j] + m2[i][j];
}
}
printf("\n\nAddition of two Matrices \n\n");
for(i=0;i<=1;i++)
{
printf("[ ");
for(j=0;j<=1;j++)
{
printf("%d ",m_add[i][j]);
}
printf("]\n");
}
getch();
}
Thanks .. nice program for beginners. As per my understanding the time complexity of adding two matrix in C is O(n^2).
ReplyDeleteVery better
ReplyDelete