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.
1# Print palindrome numbers2print("Palindrome numbers 1-500:")3for num in range(1, 501):4 # Reverse number5 original = num6 reversed_num = 07 temp = num8 while temp > 0:9 reversed_num = reversed_num * 10 + temp % 1010 temp //= 101112 # Check palindrome13 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
Visit candidates one through five hundred.
- 2
Reverse a copy into a fresh accumulator.
- 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.
๐Related Content
- ๐
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- ๐
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- ๐ป
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum