C Program to Add Two Matrices

Add Two Matrices :

On this page  we will write a basic C program to add two matrices for understanding basic structure of multidimensional array in C programming. In programming , we can add two or more matrices  easily by using proper syntax and algorithm.

C Program to Add Two Matrices

Algorithm For C Program to Add Two Matrices :

To add two matrices the number of row and number of columns in both the matrices must be same the only addition can take place.

Step 1: Start

Step 2: Declare matrix mat1[row][col];
             and matrix mat2[row][col];
             and matrix sum[row][col]; row= no. of rows, col= no. of columns

Step 3: Read row, col, mat1[][] and mat2[][]

Step 4: Declare variable i=0, j=0

Step 5: Repeat until i < row

         5.1: Repeat until j < col

                 sum[i][j]=mat1[i][j] + mat2[i][j]

                 Set j=j+1

         5.2: Set i=i+1

Step 6: sum is the required matrix after addition

Step 7: Stop

In the above algorithm,

  • using scanf( )  will take the input from the user rows,columns,elements of both the matrix.
  • we will the add the element of matrix 1 and matrix 2 in third matrix let say sum matrix.
  • printf( )  will print the output on the screen
  • we have to use the for loop to input the element of matrix ,sum the elements of matrices and print the final result.

Example: C Program to Add Two Matrices :

Run
#include <stdio.h>
int main ()
{
  int mat1[3][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8} };
  int mat2[3][3] = { {9, 10, 11}, {12, 13, 14}, {15, 16, 17} };
  int sum[3][3], i, j;

  printf ("matrix 1 is :\n");
  for (i = 0; i < 3; i++)
    {
      for (j = 0; j < 3; j++)
	{
	  printf ("%d   ", mat1[i][j]);
	  if (j == 3 - 1)
	    {
	      printf ("\n\n");
	    }
	}
    }

  printf ("matrix 2 is :\n");
  for (i = 0; i < 3; i++)
    {
      for (j = 0; j < 3; j++)
	{
	  printf ("%d   ", mat2[i][j]);
	  if (j == 3 - 1)
	    {
	      printf ("\n\n");
	    }
	}
    }
  // adding two matrices
  for (i = 0; i < 3; i++)
    for (j = 0; j < 3; j++)
      {
	sum[i][j] = mat1[i][j] + mat2[i][j];
      }

  // printing the sum 0f two matrices
  printf ("\nSum of two matrices: \n");
  for (i = 0; i < 3; i++)
    {
      for (j = 0; j < 3; j++)
	{
	  printf ("%d   ", sum[i][j]);
	  if (j == 3 - 1)
	    {
	      printf ("\n\n");
	    }
	}
    }

  return 0;
}

Output :

matrix 1 is :
0   1   2   

3   4   5   

6   7   8   

matrix 2 is :
9   10   11   

12   13   14   

15   16   17   


Sum of two matrices: 
9   11   13   

15   17   19   

21   23   25   


Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription