Basics
Arithmetic
Division, Modulo, Power
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
a ← 7
3integer :: a, b, q, r, p4a = 75b = 2values this step7ab ← 2
4a = 75b = 26q = a / bvalues this step2bq ← 3
5b = 26q = a / b7r = mod(a, b)values this step3q7a2br ← 1
6q = a / b7r = mod(a, b)8p = a ** bvalues this step1r7a2bp ← 49
7r = mod(a, b)8p = a ** b9print '(I0, A, I0, A, I0)', q, " ", r, " ", pvalues this step49p7a2bprint '(I0, A, I0, A, I0)', q, " ", r, " ", p
8 p = a ** b9 print '(I0, A, I0, A, I0)', q, " ", r, " ", p10end program arithmeticoutput3 1 49values this step3q1r49p
Follow the Math
astarts at7andbstarts at2.q = a / buses integer division, so7 / 2becomes3.r = mod(a, b)keeps the remainder, which is1.p = a ** bsquares7to get49.- 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.