An outer FOR-NEXT loop employs variable num to run through all possible 5
digit integers, that is, from 10,000 to 99,999. Each value of num is submitted as the parameter
to ReverseInteger().
This function does what its name implies; it reverses whatever argument
it receives and returns the reversed value to the calling
routine. The returned value is stored in variable revNum.
Variable flag is going to be
used to check whether products of revNum
and multiplying digits are greater than 99,999. The convention adopted
is that when flag equals 0
the multiplication is ok, and when it equals -1 the multiplication
creates too big a number. So we begin each investigation by setting flag equal to zero.
Variable it is going to be
used as the multiplying digit for each value of revNum to be tested. It will be
cycled through the values 2 to 9 inclusive, and is initially set to 2.
Once flag and it have been initialised, the
program can enter its main DO loop. As each value of it is multiplied with revNum, the result is stored in
variable prod. If prod's value turns out to be
greater than 99,999, flag is
set to -1 to indicate further investigation won't be required.
If prod wasn't greater than
99,999, it's compared with revNum
in the line if prod = revNum. If they are equal, of course, we've found a
solution, since the reverse of the 5-digit integer in num multiplied by a single-digit
integer has produced num
itself, and this result is printed to the screen.
Within the DO loop, the rest of the single-digit values for it are tried in case some other
result exists for the value in num.
The loop makes two tests before being executed again: until flag = -1 | it = 10. If flag is set to -1 there's no point
in continuing with further values of it,
since they will all make the same failure - prod greater than
99,999. And if it is
now equal to 10, no further increments are required. If either of
these tests are met, the next value for num is fetched and processed by the
outer FOR loop. This continues until num
is greater than 99,999.