Algorithms & Flowcharts — Worked Examples

7. Number-Crunching Algorithms

Number-Crunching Algorithms

These algorithms take an integer apart one digit at a time. Two tiny operations do almost all the work, so learn them cold:

  • n % 10 (remainder when dividing by 10) gives the last digit. Example: 1234 % 10 = 4.
  • n // 10 (whole-number division by 10) chops off the last digit. Example: 1234 // 10 = 123.

Repeating "grab last digit, then chop it off" peels a number down to nothing. Every trace table below has a % 10 column and a // 10 column so you can see each digit fall off.

Example (a): Count the digits in an integer

Problem

Read a non-negative integer and count how many digits it has (for example 4705 has 4 digits).

Start INPUT n count = 0 is n > 0 ? Yes count = count + 1 n = n // 10 loop back No OUTPUT count End

Pseudocode

INPUT n
SET count = 0
WHILE n > 0
    SET count = count + 1
    SET n = n // 10
OUTPUT count

Trace table

Sample input: n = 4705. Each iteration chops one digit off with // 10.

Iterationn (before)n > 0 ?count after (+1)n // 10n (after)
start4705-0-4705
14705Yes14705 // 10470
2470Yes2470 // 1047
347Yes347 // 104
44Yes44 // 100
exit0No4-0

Digit count: 4.

In Python

n = 4705
count = 0
while n > 0:
    count = count + 1
    n = n // 10
print(count)

# Output:
# 4

Example (b): Sum of the digits of an integer

Problem

Add together all the digits of an integer (for 4705 that is 4+7+0+5 = 16).

Start INPUT n sum = 0 is n > 0 ? Yes sum = sum + (n % 10) n = n // 10 loop back No OUTPUT sum End

Pseudocode

INPUT n
SET sum = 0
WHILE n > 0
    SET digit = n % 10
    SET sum = sum + digit
    SET n = n // 10
OUTPUT sum

Trace table

Sample input: n = 4705. The sum column is the accumulator.

Iterationn (before)digit = n % 10sum = sum + digitsum aftern = n // 10
start4705--04705
1470550 + 55470
247005 + 0547
34775 + 7124
44412 + 4160
exit0--160

Digit sum: 16.

In Python

n = 4705
total = 0
while n > 0:
    digit = n % 10
    total = total + digit
    n = n // 10
print(total)

# Output:
# 16

Example (c): Reverse an integer

Problem

Turn an integer back to front, so 1234 becomes 4321.

The trick: peel the last digit off the input with % 10, and push it onto a growing answer by doing rev = rev * 10 + digit. Multiplying rev by 10 shifts its digits left to make room for the new one.

Start INPUT n rev = 0 is n > 0 ? Yes rev = rev * 10 + (n % 10) n = n // 10 loop back No OUTPUT rev End

Pseudocode

INPUT n
SET rev = 0
WHILE n > 0
    SET digit = n % 10
    SET rev = rev * 10 + digit
    SET n = n // 10
OUTPUT rev

Trace table

Sample input: n = 1234.

Iterationn (before)digit = n % 10rev = rev*10 + digitrev aftern = n // 10
start1234--01234
1123440*10 + 44123
212334*10 + 34312
312243*10 + 24321
411432*10 + 143210
exit0--43210

Reversed number: 4321.

In Python

n = 1234
rev = 0
while n > 0:
    digit = n % 10
    rev = rev * 10 + digit
    n = n // 10
print(rev)

# Output:
# 4321

Example (d): Check if a number is a palindrome

Problem

A palindrome reads the same forwards and backwards (like 1221 or racecar). Decide whether an integer is a palindrome by reversing it and comparing with the original.

Start INPUT n original = n , rev = 0 is n > 0 ? Yes rev = rev*10 + (n % 10) n = n // 10 loop back No rev == original ? Yes No OUTPUT yes OUTPUT no End

Pseudocode

INPUT n
SET original = n
SET rev = 0
WHILE n > 0
    SET rev = rev * 10 + (n % 10)
    SET n = n // 10
IF rev == original
    OUTPUT "palindrome"
ELSE
    OUTPUT "not a palindrome"

Trace table

Sample input: n = 1221 (we save original = 1221 first).

Iterationn (before)digit = n % 10rev = rev*10 + digitrev aftern = n // 10
start1221--01221
1122110*10 + 11122
212221*10 + 21212
312212*10 + 21221
411122*10 + 112210
exit0--12210

Final check: rev (1221) == original (1221) is Yes, so 1221 is a palindrome.

In Python

n = 1221
original = n
rev = 0
while n > 0:
    rev = rev * 10 + (n % 10)
    n = n // 10
if rev == original:
    print("palindrome")
else:
    print("not a palindrome")

# Output:
# palindrome