---
title: "Add and subtract matrices in FORTRAN"
subject: "Computer Programming"
chapter: 11
key: "semester_1/computer_programming:fortran-matrix-add-subtract"
source: https://www.bctengineer.com/q/semester_1/computer_programming/fortran-matrix-add-subtract
site: BCT Engineering
---

# Add and subtract matrices in FORTRAN

Computer Programming · Chapter 11: Programming Language: FORTRAN

- Asked in: 2072 Chaitra, 7 marks
- Video: [Matrix Addition, Subtraction & Multiplication | FORTRAN](https://www.youtube.com/watch?v=TIfwcv2PWGs) — Bisank's Ultimate Guide
- 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-add-subtract
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-11

## Question

Write a FORTRAN program to add and subtract two matrices and display the results in matrix form.

## Short answer

Read rows, columns, and matrices using nested implied DO. Compute sum S(i,j)=A(i,j)+B(i,j) and difference D(i,j)=A(i,j)-B(i,j) in loops. Display each matrix row by row with FORMAT. For A=[1 2;3 4], B=[5 6;7 8], sum is [6 8;10 12] and difference [-4 -4;-4 -4].

## Answer

**Method.** Matrices of the same order r × c are read row by row with a nested implied DO. The sum is S(i,j) = A(i,j) + B(i,j) and the difference is D(i,j) = A(i,j) − B(i,j). Each result is printed one row per line using a FORMAT statement.

```fortran
      PROGRAM MATADS
      INTEGER A(10,10), B(10,10), S(10,10), D(10,10)
      INTEGER R, C, I, J
      PRINT *, 'ENTER ROWS AND COLUMNS'
      READ *, R, C
      PRINT *, 'ENTER FIRST MATRIX'
      READ *, ((A(I,J), J = 1, C), I = 1, R)
      PRINT *, 'ENTER 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)
            D(I,J) = A(I,J) - B(I,J)
   10    CONTINUE
   20 CONTINUE
      PRINT *, 'SUM OF THE MATRICES'
      DO 30 I = 1, R
         WRITE(*,100) (S(I,J), J = 1, C)
   30 CONTINUE
      PRINT *, 'DIFFERENCE OF THE MATRICES'
      DO 40 I = 1, R
         WRITE(*,100) (D(I,J), J = 1, C)
   40 CONTINUE
  100 FORMAT(10I5)
      STOP
      END
```

**Sample:** for A = [1 2; 3 4] and B = [5 6; 7 8], the sum is [6 8; 10 12] and the difference is [-4 -4; -4 -4]. Note that in `READ *, ((A(I,J), J=1,C), I=1,R)` the elements are typed row by row.

## Check yourself

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

1. What condition must the two matrices satisfy?
   A. The same order r x c
   B. Square matrices only
   C. Different orders
   D. The first has fewer rows
   Answer: A. Addition and subtraction are element by element, so both matrices must have the same order. (Key point: Condition for addition)

2. Which formulas give the sum and difference?
   A. S(i,j) = A(i,j) * B(i,j); D = A / B
   B. S(i,j) = A(i,j) + B(i,j); D(i,j) = A(i,j) - B(i,j)
   C. S(i,j) = A(j,i) + B(i,j)
   D. S(i,j) = A(i,j) + B(j,i)
   Answer: B. Corresponding elements are added or subtracted. (Key point: Element formulas)

3. In READ *, ((A(I,J), J=1,C), I=1,R), in what order is the data typed?
   A. Column by column
   B. Row by row
   C. Diagonal first
   D. Random order
   Answer: B. The inner variable J varies fastest, so the elements of row 1 are entered first, then row 2. (Key point: Reading order)

4. For A = [1 2; 3 4] and B = [5 6; 7 8], what is A - B?
   A. [4 4; 4 4]
   B. [-4 -4; -4 -4]
   C. [6 8; 10 12]
   D. [-1 -1; -1 -1]
   Answer: B. Each element is 4 less than the corresponding element of B, giving -4 everywhere. (Key point: Sample difference)

## Related in Computer Programming

- [Add two matrices](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Amatrix-add-two) — Computer Programming, chapter 7
- [Sum of two matrices in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-matrix-sum) — 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
- [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
- [Structure of a FORTRAN program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-structure) — Computer Programming, chapter 11, asked 5×
- [Two dimensional arrays in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-2d-array-syntax) — Computer Programming, chapter 11

## Related in other subjects

- [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
- [Stiffness matrix: element matrices, member forces, BMD](https://www.bctengineer.com/question.md?key=semester_5%2Ftheory_of_structures_ii%3Astiffness-element-matrices-600kn-bmd) — Theory of Structures II, chapter 3
