ADDITION OF TWO MATRICES USING C PROGRAM




C program for addition of two matrices using arrays source code. Matrix addition in c language:


C code:

#include<stdio.h>
int main(){
  int a[3][3],b[3][3],c[3][3],i,j;
  printf("Enter the First matrix->");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);
  printf("\nEnter the Second matrix->");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&b[i][j]);
  printf("\nThe First matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }
  printf("\nThe Second matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
      printf("%d\t",b[i][j]);
   }
   for(i=0;i<3;i++)
       for(j=0;j<3;j++)
            c[i][j]=a[i][j]+b[i][j];
   printf("\nThe Addition of two matrix is\n");
   for(i=0;i<3;i++){
       printf("\n");
       for(j=0;j<3;j++)
            printf("%d\t",c[i][j]);
   }
   return 0;
}



Algorithm:


Addition of two matrices:

Rule: Addition of two matrices is only possible if both matrices are of same size.

Suppose two matrices A and B is of same size m X n

Sum of two matrices is defined as

(A + B)ij  = Aij + Bij
Where 1 i  m and 1 ≤ j  n

For example:
Suppose two matrices A and B of size of 2 X 3 is as follow:

 
       





27 comments:

  1. why not published the algorithm.

    ReplyDelete
    Replies
    1. the program is infront of you

      Delete
  2. thanks bro........
    jai maharashtra

    ReplyDelete
  3. thank u so much

    ReplyDelete
  4. i think its wrong

    ReplyDelete
  5. very-2 gud solution of dda example of c lang.

    ReplyDelete
  6. You have just saved my life.
    Thank you!

    ReplyDelete
  7. thak you SO much!!
    I had a doubt on what gets added with what..
    thnx 4 the crystal clear expanation!

    ReplyDelete
  8. NICE ONE. THANX FOR D HELP

    ReplyDelete
  9. mass!!!thanks...

    ReplyDelete
  10. I am P.Gowrimanogari i have one doubt in c program if i give my name means in input means the entier output screen have to display as for eg: GOWRI .can u tell what concept i have to use????

    ReplyDelete
    Replies
    1. you can easily input a string and print it using %s specifier and use loop to display it many times....
      thanks.....

      Delete
  11. Shouldn't you check first that whether the give 2 matrices are equal using if-else statements by comparing rows and columns? As only 2 matrice with same no of rows and columns can be added.

    ReplyDelete
  12. Ran the program.It ran into infinite loop

    ReplyDelete
  13. void main()
    {
    int num[2][3],r,c;
    clrscr();
    for(r=0; r<2; r++)
    {
    for(c=0; c<3; c++)
    {
    printf("\n enter no.");
    scanf("%d",&num[r][c]);
    }
    }
    for(r=0; r<2; r++)
    {
    printf("\n");
    for(c=0; c<3; c++)
    {
    printf("%d",num[r][c]);
    }
    }
    getch();
    }

    ReplyDelete
  14. enter no.7

    enter no.6

    enter no.5

    enter no.4

    enter no.3

    enter no.2

    765
    432









    ReplyDelete
  15. good..thank u so much

    ReplyDelete
  16. how can we add 2 matrices without using forloop...........?

    ReplyDelete
  17. i want the prog for square matrix can anyone help me?

    ReplyDelete
  18. can you please drow flow charT ?/

    ReplyDelete
  19. excellent thank u soo much

    ReplyDelete