C Program to Add Two Matrices

Matrix is an important chapter in the Mathematics that we study in 9th Grade. Adding Two Matrices is very simple, it is the same as simple Addition. In this C Programming example, we will write a C program to add two Matrices.

To begin with, we must have the knowledge of the following concepts:

  • Array
  • Two-Dimensional Array

Algorithm to Add Two Matrices

Here’s the algorithm to add two Matrices:

  1. START
  2. Input the matrix 1 elements.
  3. Input the matrix 2 elements.
  4. Repeat from i = 0 to 3
  5. Repeat from j = 0 to 3
  6. mat3[i][j] = mat1[i][j] + mat2[i][j]
  7. Print mat3.
  8. STOP

C Program to Add Two Matrices

Here’s the C Program to add two Matrices:

#include<stdio.h>
#include<conio.h>
int main()
{
 int i, j, mat1[3][3], mat2[3][3];
 printf("Enter first matrix: \n");
 for(i = 0; i < 3; i++)
 {
 for(j = 0; j < 3; j++)
 {
 scanf("%d", &mat1[i][j]);
 } 
 }
 printf("\nEnter second matrix: \n");
 for(i = 0; i < 3; i++)
 {
 for(j = 0; j < 3; j++)
 { 
 scanf("%d", &mat2[i][j]);
 } 
 }
 printf("\nmat1 + mat2 = \n");
 for(i = 0; i < 3; i++)
 {
 for(j = 0; j < 3; j++)
 {
 printf("%d ", mat1[i][j] + mat2[i][j]); 
 } 
 printf("\n");
 } 
 getch();
}

Output

Here’s the output of the above program:

Default image
groot

Leave a Reply

Your email address will not be published.