Posted in: C Language

Write a program in C to input and add two matrix

In this C program we will get two matrix as input from user and the program will calculate the sum of both matrix and display the sum as output.

For writing this program, you need to have knowlegde of following concepts

  1. Array
  2. Two-Dimensional Array

Algorithm to input and add two matrix and display as output

  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 input and add two matrix and display as output

#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 of Program

C Program to Input and Add Two Matrix
Output of above program

Download Files:

  1. Raw Code
  2. C Program File

Comment (1) on "Write a program in C to input and add two matrix"

Leave a Reply

Your email address will not be published.