9.12.2011

Addition of two 3x3 matrix

Q. Write a C program for addition of two 3x3 matrix.

Ans.

/*source code of addition of 3x3 matrix c program*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int matA[3][3],matB[3][3],matC[3][3];
 int r,c,k;
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    printf("Enter first matrix : ");
    scanf("%d", &matA[r][c]);
  }
 }
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    printf("Enter second matrix : ");
    scanf("%d", &matB[r][c]);
  }
 }
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    matC[r][c]=0;
    for(k=0; k<3;k++)
       matC[r][c] = matA[r][c] + matB[r][c];
  }
 }
 printf("\n New addition matrix : \n"); 
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
     printf(" %d",matC[r][c]);
  printf("\n");
 }
 getch();
 return 0;
}


/************************************************************
The output of above Addition of two 3x3 martix C program would be:
***********************************************************/
Output of addition of two 3x3 matrix  C program
Figure: Screen shot for addition of two 3x3 matrix
C program

In simple and easy way output of above program :

Enter first matrix :
  1 2 3
  4 5 6
  7 8 9
Enter second matrix :
  2 1 7
  4 6 3
  8 1 1

New addition matrix :
  3  3  10
  8  11 9
  15 9  10


You might also like to read following matrix C program:

    1.  Addition of two 3x3 matrix using function
   2. Product of two matrix
   3. Transpose of two matrix
   4. Diagonal sum of two matrix
   5. Subtract of two matrix

6 comments:

  1. Try to change the code...there are logical errors

    ReplyDelete
  2. This code is very easy...............

    ReplyDelete
  3. This good but u need to change the background colour

    ReplyDelete
    Replies
    1. @Srinidhi
      Backgroud color has been changed.

      Delete
  4. Can you please explain the need of third for loop in the addition of matrices because it gives the same result without using the third then what is the difference between with and without using third loop.

    ReplyDelete
  5. Can you please give me reason to use third for loop in addition of the two matrices because it gives the same result without using third loop.

    ReplyDelete