---
title: "Sum of two matrices in FORTRAN"
subject: "Computer Programming"
chapter: 11
key: "semester_1/computer_programming:fortran-matrix-sum"
source: https://www.bctengineer.com/q/semester_1/computer_programming/fortran-matrix-sum
site: BCT Engineering
---

# Sum of two matrices in FORTRAN

Computer Programming · Chapter 11: Programming Language: FORTRAN

- Asked in: 2068 Shrawan, 8 marks
- Video: [009 : FORTRAN PROGRAM TO ADD TWO MATRIX](https://www.youtube.com/watch?v=7jM7fJ14jDw) — adventure in mathematics
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/fortran-matrix-sum
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-11

## Question

Write a program in FORTRAN to read two matrices from user, find their sum and display the sum.

## Short answer

Read the order and both matrices using nested implied DO. Add corresponding elements: S(i,j)=A(i,j)+B(i,j). Print the sum row by row with FORMAT.

## Answer

**Method.** Read the order (rows R, columns C), then read both matrices with a nested implied DO (the inner variable J varies fastest, so the data is entered row by row). The two matrices must have the same order. Add corresponding elements in nested DO loops and display the sum, one row per line.

```fortran
      PROGRAM MATSUM
      INTEGER A(10,10), B(10,10), S(10,10)
      INTEGER R, C, I, J
      PRINT *, 'ENTER NUMBER OF ROWS AND COLUMNS'
      READ *, R, C
      PRINT *, 'ENTER ELEMENTS OF FIRST MATRIX'
      READ *, ((A(I,J), J = 1, C), I = 1, R)
      PRINT *, 'ENTER ELEMENTS OF SECOND MATRIX'
      READ *, ((B(I,J), J = 1, C), I = 1, R)
      DO 20 I = 1, R
         DO 10 J = 1, C
            S(I,J) = A(I,J) + B(I,J)
   10    CONTINUE
   20 CONTINUE
      PRINT *, 'SUM OF THE TWO MATRICES'
      DO 30 I = 1, R
         WRITE(*,100) (S(I,J), J = 1, C)
   30 CONTINUE
  100 FORMAT(10I6)
      STOP
      END
```

**Sample:** A = [1 2 3; 4 5 6], B = [6 5 4; 3 2 1] gives
```text
     7     7     7
     7     7     7
```

## Check yourself

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

1. Why must both matrices have the same order?
   A. Because elements are added corresponding position by position
   B. Because the FORMAT needs it
   C. Because arrays must be 10 x 10
   D. Because loops start at 1
   Answer: A. Each S(i,j) needs both A(i,j) and B(i,j) to exist. (Key point: Same order)

2. How is the sum computed?
   A. Nested DO loops with S(I,J) = A(I,J) + B(I,J)
   B. A single DO with S = A + B on two numbers
   C. By multiplying rows by columns
   D. By transposing B first
   Answer: A. Two nested DO loops go through every row I and column J and add corresponding elements. (Key point: Nested loops)

3. In the nested implied DO, why is data entered row by row?
   A. The inner variable J changes fastest
   B. The outer variable I changes fastest
   C. FORTRAN reads columns only
   D. The READ is unformatted
   Answer: A. In ((A(I,J), J=1,C), I=1,R) J is the inner loop, so a full row is read before I changes. (Key point: Entering data)

4. For A = [1 2 3; 4 5 6] and B = [6 5 4; 3 2 1], what is the sum?
   A. All elements 7
   B. All elements 0
   C. [7 7 7; 7 7 7] only for first row
   D. All elements 12
   Answer: A. 1+6, 2+5, 3+4, 4+3, 5+2, 6+1 all equal 7. (Key point: Sample result)

## Related in Computer Programming

- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
- [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
- [Add two matrices](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Amatrix-add-two) — Computer Programming, chapter 7
- [Structure of a FORTRAN program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-structure) — Computer Programming, chapter 11, asked 5×
- [Series sum 1/1¹+1/2²+… in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-series-sum-1-over-n) — Computer Programming, chapter 11
- [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

- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [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
- [8086 ALP: display only alphabetic characters](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-alphabetic-only) — Microprocessor, chapter 3
- [8086 ALP: count vowels and display string](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-count-vowels-display) — Microprocessor, chapter 3
