MASTER YOUR LOGIC BUILDING:Phase 2 - Level 2: Number-based Looping Logic

Mastering phase 2 - level 2: number-based looping logic concepts and implementation.

Phase 2 - Level 2: Number-based Looping Logic

Why This Matters

Your phone PIN has digits you might want to count, reverse, or check. Bank systems validate account numbers digit by digit. Loops plus number tricks let you process every digit of a number automatically — no matter how long it is.

This level combines what you learned about digits (Phase 1) with loops (Phase 2).

What You'll Learn

  • How to count digits in any number using a loop
  • How to reverse a number digit by digit
  • The "extract last digit, shrink the number" pattern
  • How to build a new number inside a loop

The Core Pattern: Process Digits One by One

Every digit problem uses the same two operations inside a loop:

last_digit = num % 10    # grab the rightmost digit
num = num // 10          # remove it from the number

Repeat until the number becomes 0.

Step-by-Step: Count the Digits

Problem: How many digits are in 12345?

  1. Start with count = 0
  2. While the number is greater than 0:
  • Add 1 to count
  • Remove the last digit
  1. Print the count
count = 0
temp = abs(num)   # handle negative numbers

while temp > 0:
    count += 1
    temp //= 10   # same as: temp = temp // 10

print(count)

Trace for 12345: 12345 → 1234 → 123 → 12 → 1 → 0. That's 5 steps → 5 digits.

Edge case: The number 0 has 1 digit. Handle it separately if needed.

Step-by-Step: Reverse a Number

Problem: Turn 12345 into 54321.

  1. Start with reversed_num = 0
  2. While the number is greater than 0:
  • Grab the last digit
  • Append it to reversed_num (multiply by 10, then add the digit)
  • Remove the last digit from the original
  1. Print the result
reversed_num = 0
temp = abs(num)

while temp > 0:
    last_digit = temp % 10
    reversed_num = reversed_num * 10 + last_digit
    temp //= 10

Why multiply by 10? It shifts existing digits left to make room for the new one — like typing digits on a calculator.

Trace for 123: 0→3, 3→32, 32→321. Done!

Common Mistakes

  • Forgetting `abs()` — negative numbers behave unexpectedly without it
  • Not handling 0 — 0 has one digit, but while temp > 0 never runs
  • Building the reversed number wrong — order matters: multiply first, then add the digit
  • Modifying the input directly — use a temp copy so you don't lose the original

Tips

  • Trace the loop on paper for a 3-digit number — you'll see the pattern clearly
  • The "count digits" and "reverse number" loops share the same skeleton — learn one, adapt the other
  • Test with: 0, single digit (7), negative (-123), and a large number

Practice Focus

Try the examples below without peeking:

  • Count how many digits a number has
  • Reverse a number's digits using the multiply-and-add trick
  • Practice tracing the loop by hand before running your code

Hands-on Examples

Count Digits in a Number

# Take a number
num = int(input("Enter a number: "))

# Count digits
count = 0
temp = abs(num)  # Handle negative numbers

if temp == 0:
    count = 1
else:
    while temp > 0:
        count += 1
        temp //= 10  # Remove last digit

print(f"Number of digits: {count}")

Repeatedly divide by 10 (integer division) to remove the last digit. Count iterations until number becomes 0.