Armstrong Number Check
Check if a number is Armstrong number.
IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 5 of 14
armstrong-number-check.py
1# Take number2num = int(input("Enter a number: "))34# Count digits5temp = abs(num)6count = 07while temp > 0:8 count += 19 temp //= 101011# Calculate sum of digits raised to power12temp = abs(num)13armstrong_sum = 014while temp > 0:15 digit = temp % 1016 armstrong_sum += digit ** count17 temp //= 101819# Check20if abs(num) == armstrong_sum:21 print("Armstrong number")22else:23 print("Not an Armstrong number")
Output
Enter a number: 153 Armstrong number Enter a number: 123 Not an Armstrong number
What's going on
Armstrong: sum of digits raised to power of digit count equals the number.
Key Concepts:
First count digits
Then calculate sum of digits^count
Compare with original number
🔗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