Monday, February 9, 2015

C program to read two matrices and add them

#include<stdio.h>
#include<conio.h>
main()
{                
/*declaration of three array with same size */
  int a[10][10],b[10][10],c[10][10],i,j,m,n;
printf("Enter the size of A and B metrix\n");
scanf("%d%d",&m,&n);
     /* reading system */
printf("Enter the elements of A metrix\n");
for(i=0;i<m;i++)
  for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of B metrix\n");
for(i=0;i<m;i++)
  for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
                /* Addition Logic */
for(i=0;i<m;i++)
  for(j=0;j<n;j++)
  c[i][j] = a[i][j]+b[i][j];
clrscr();
           /*display entire matrix */
printf("\nA matrix is :\n\n");
for(i=0;i<m;i++)
{
   for(j=0;j<n;j++)
printf("%4d", a[i][j]);
   printf("\n");
}
printf("\nB matrix is :\n\n");
 
for(i=0;i<m;i++)
{
   for(j=0;j<n;j++)
printf("%4d", b[i][j]);
   printf("\n");
}
 
printf("\nC matrix is :\n\n");
for(i=0;i<m;i++)
{
     for(j=0;j<n;j++)
printf("%4d", c[i][j]);
printf("\n");
}
 
getch();
}

No comments:

Post a Comment