Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

All Palindrome Numbers 1-500

Find every numeric palindrome from one through five hundred. Students rebuild each candidate's reverse and reset temporary values within the outer loop.

AdvancedPhase 2 - Level 5: Logical Loop CombinationsExample 3 of 12
all-palindrome-numbers-1-500.py
1# Print palindrome numbers
2print("Palindrome numbers 1-500:")
3for num in range(1, 501):
4 # Reverse number
5 original = num
6 reversed_num = 0
7 temp = num
8 while temp > 0:
9 reversed_num = reversed_num * 10 + temp % 10
10 temp //= 10
11
12 # Check palindrome
13 if original == reversed_num:
14 print(num, end=" ")
15print()

Output

...

The sample abbreviates a longer result. The program prints the intervening values; it does not print these dots.

In this one

  • Reset numeric reversal
  • Preserve each candidate
  • Filter a bounded range

How it works

  1. 1

    Visit candidates one through five hundred.

  2. 2

    Reverse a copy into a fresh accumulator.

  3. 3

    Print candidates equal to their reverses.

Edge cases

Does 1 count?

Does 1 count?

Yes. A single digit reads the same backward.

Does 500 count?

Does 500 count?

No. Its numeric reverse is five.

What's going on

The outer loop chooses a candidate; the inner digit loop reverses a copy of it. original keeps the unchanged candidate for comparison.

Each match is printed once. Single-digit numbers pass naturally, as do values such as 121. reversed_num must restart at zero for every candidate, or digits from the previous reversal would contaminate the next decision.

Does 1 count? Yes. A single digit reads the same backward.

Does 500 count? No. Its numeric reverse is five.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why keep original?

Pick an answer to check it.

Question 2

Does 1 count?

Pick an answer to check it.

Questions people ask

Does 1 count?

Yes. A single digit reads the same backward.

Does 500 count?

No. Its numeric reverse is five.

Why keep original?

The working copy is shortened to zero.