Example HTML page

C Program for addition of Two matrices.

In this section, we are writing a program for adding two matrices of order mXn.

For example, if the order of two matrices are 2X2.

Then, to do the addition we require two input matrices and to store the result one result matrix is required.

For example:

First matrix:

1 2

3 4

Second matrix:

3 4

1 2

result matrix:

4 6

4 6

To write this program, do the following things.

Step 1: read the order of the matrix

Step 2: read the first matrix

Step 3: read the second matrix

Step 4: write the logic for addition and store it in result matrix.

Step 5: print the result matrix.

#include <stdio.h>
  
int main()
{
   int  first[10][10], second[10][10], result[10][10],m, n, i,j;
   // read the order of the matrix.
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
    
   // reading the first matrix 
   printf("Enter the elements of first matrix\n");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         scanf("%d", &first[i][j]);
          
  // reading the second matrix
   printf("Enter the elements of second matrix\n");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         scanf("%d", &second[i][j]);
          
 // addition logic
   printf("Sum of entered matrices:-\n");
    
   for (i = 0; i < m; i++)
   {
    for (j = 0; j < n; j++)
        {
            result[i][j]=first[i][j]+second[i][j];
        }
   }
        
 // printing the elements of resultant matrix
   for (i=0;i<m;i++)
   {
    for (j=0;j<n;j++)
    {
        printf("%d ",result[i][j]);
       }
       printf("\n");
   }
  
   return 0;
}

Matrix Addition

Output:

Command Prompt

Enter the number of rows and columns of matrix
3
3
Enter the elements of first matrix
1 2 3
1 4 3
1 2 4
Enter the elements of second matrix
2 3 1
1 2 3
3 2 1
Sum of entered matrices:-
3 5 4
2 6 6
4 4 5

Example HTML page

Leave a Reply

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

Pin It on Pinterest