---
title: "Add two matrices"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:matrix-add-two"
source: https://www.bctengineer.com/q/semester_1/computer_programming/matrix-add-two
site: BCT Engineering
---

# Add two matrices

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2070 Asar, 6 marks
- Video: [Write a C Program for Addition of TWO Matrices using Arrays](https://www.youtube.com/watch?v=0tHyBnq4P-Q) — Code with Viju
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/matrix-add-two
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a C program to read two matrices from user, add them and display the result in matrix form.

## Short answer

Two matrices of same order are added element-wise: s[i][j] = a[i][j] + b[i][j]. The program reads dimensions and elements, computes sum in nested loops, and displays the sum matrix. Ensure matching rows and columns before addition.

## Answer

Two matrices can be added only if they have the same order (rows and columns). The sum matrix S has s[i][j] = a[i][j] + b[i][j]. Nested loops read, add and print the matrices.

```c
#include <stdio.h>

int main(void)
{
    int a[10][10], b[10][10], s[10][10], r, c, i, j;
    printf("Enter rows and columns: ");
    scanf("%d %d", &r, &c);
    printf("Enter first matrix:\n");
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++)
            scanf("%d", &a[i][j]);
    printf("Enter second matrix:\n");
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++)
            scanf("%d", &b[i][j]);

    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++)
            s[i][j] = a[i][j] + b[i][j];

    printf("Sum of the matrices:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++)
            printf("%5d", s[i][j]);
        printf("\n");
    }
    return 0;
}
```

**Sample run**

Input: `2 3 1 2 3 4 5 6 6 5 4 3 2 1`

```text
Enter rows and columns: Enter first matrix:
Enter second matrix:
Sum of the matrices:
    7    7    7
    7    7    7
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When can two matrices be added?
   A. When they have the same order (rows and columns)
   B. When they are square
   C. When columns of first equal rows of second
   D. Always
   Answer: A. Addition is element by element and needs matching dimensions. (Key point: Condition for addition)

2. How is each element of the sum matrix formed?
   A. s[i][j] = a[i][j] * b[j][i]
   B. s[i][j] = a[i][j] + b[j][i]
   C. s[i][j] = a[i][j] + b[i][j]
   D. s[i][i] = a[i][j] + b[i][j]
   Answer: C. Corresponding elements are added. (Key point: Rule)

3. What is used to read, add and print matrices?
   A. Nested loops over rows and columns
   B. A single loop only
   C. Recursion
   D. A string function
   Answer: A. Two indices need nested loops. (Key point: Loops)

## Related in Computer Programming

- [Add and subtract matrices in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-matrix-add-subtract) — Computer Programming, chapter 11
- [Sum of two matrices in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-matrix-sum) — Computer Programming, chapter 11
- [Array overflow/underflow; add two arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-overflow-underflow-add) — Computer Programming, chapter 7
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Significance of arrays; multiply 3×3 matrices](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-significance-multiply-matrices) — Computer Programming, chapter 7
- [Transpose m×n matrix in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-matrix-transpose) — Computer Programming, chapter 11

## Related in other subjects

- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [Add objects of two different classes](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-add-two-class-objects) — Object Oriented Programming, chapter 4, asked 2×
- [Time class: add two objects](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-class-add-objects) — Object Oriented Programming, chapter 1
- [8085 ALP: add two tables, sum and carry](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-add-two-tables) — Microprocessor, chapter 2
