Integer division truncates toward zero, mod returns the remainder, and ** raises to a power.

Program

Play the program to compute a quotient, remainder, and square.

arithmetic.f90
Replay: real traced execution (multi-file project)
program arithmetic
    implicit none
    integer :: a, b, q, r, p
    a = 7
    b = 2
    q = a / b
    r = mod(a, b)
    p = a ** b
    print '(I0, A, I0, A, I0)', q, " ", r, " ", p
end program arithmetic
  1. a ← 7

    3integer :: a, b, q, r, p4a = 75b = 2
    values this step7a
  2. b ← 2

    4a = 75b = 26q = a / b
    values this step2b
  3. q ← 3

    5b = 26q = a / b7r = mod(a, b)
    values this step3q7a2b
  4. r ← 1

    6q = a / b7r = mod(a, b)8p = a ** b
    values this step1r7a2b
  5. p ← 49

    7r = mod(a, b)8p = a ** b9print '(I0, A, I0, A, I0)', q, " ", r, " ", p
    values this step49p7a2b
  6. print '(I0, A, I0, A, I0)', q, " ", r, " ", p

    8    p = a ** b9    print '(I0, A, I0, A, I0)', q, " ", r, " ", p10end program arithmetic
    output3 1 49
    values this step3q1r49p

Follow the Math

  1. a starts at 7 and b starts at 2.
  2. q = a / b uses integer division, so 7 / 2 becomes 3.
  3. r = mod(a, b) keeps the remainder, which is 1.
  4. p = a ** b squares 7 to get 49.
  5. The program prints 3 1 49. | calculation | result | printed position | | --- | --- | --- | | 7 / 2 | 3 | first | | mod(7, 2) | 1 | second | | 7 ** 2 | 49 | third |
integer division `7 / 2` truncates to `3`.
mod `mod(7, 2)` returns the remainder `1`.
exponent `a ** b` raises `a` to the power `b`.

Exercise: arithmetic.f90

Reproduce the output 3 1 49, then change b and predict the quotient, remainder, and power before running it.