Program to perform addition of two 2 by 2 matrix

An array is a collective name given to a group of similar variables. In this program we will accept two 2-D array (2 by 2 Matrix) as input from the user. This program will then perform addition of two 2 by 2 matrix. It will then display the result after adding two matrix.

Here is the program for matrix addition. If you found this article useful, do post a comment.

#include
#include

void main(){
 int arr1[10][10], arr2[10][10];
 int arr3[][3]={
   {0,0,0},
   {0,0,0},
   {0,0,0}
   };
 int i, j;
 clrscr();
 printf("Enter 3x3 array 1:\n");
 for(i=0;i<3;i++){
  for(j=0;j<3;j++){
   printf("Enter element %d x %d:",i,j);
   scanf("%d",&arr1[i][j]);
  }
 }
 printf("Enter 3x3 array 2:\n");
 for(i=0;i<3;i++){
  for(j=0;j<3;j++){
   printf("Enter element %d x %d:",i,j);
   scanf("%d",&arr2[i][j]);
  }
 }

 for(i=0;i<3;i++){
  for(j=0;j<3;j++){
   arr3[i][j]=arr1[i][j] + arr2[i][j];
  }
 }

 printf("Addition of Array 1 and Array 2 is: \n");
 for(i=0;i<3;i++){
  for(j=0;j<3;j++){
   printf("\t%d",arr3[i][j]);
  }
  printf("\n");
 }
getch();
}

Output:

Enter 3x3 array 1:
Enter element 0 x 0:1                                                           
Enter element 0 x 1:1                                                           
Enter element 0 x 2:1                                                           
Enter element 1 x 0:1                                                           
Enter element 1 x 1:1                                                           
Enter element 1 x 2:1                                                           
Enter element 2 x 0:1                                                           
Enter element 2 x 1:1                                                           
Enter element 2 x 2:1                                                           
Enter 3x3 array 2:                                                              
Enter element 0 x 0:1                                                           
Enter element 0 x 1:1                                                           
Enter element 0 x 2:1                                                           
Enter element 1 x 0:1                                                           
Enter element 1 x 1:1                                                           
Enter element 1 x 2:1                                                           
Enter element 2 x 0:1                                                           
Enter element 2 x 1:1                                                           
Enter element 2 x 2:1                                                           
Addition of Array 1 and Array 2 is:
 2       2       2
 2       2       2
 2       2       2

Also See:
2-Dimensional Array in C Programming Language

You may also like...

2 Responses

  1. Anonymous says:

    is it mandatory to initialize the arr3 or we can get the results without it

  2. Anonymous says:

    same question as above

Leave a Reply

Your email address will not be published. Required fields are marked *