Checking Armstrong Numbers in Python

Day 5/365: Checking Armstrong Numbers in Python 🔢🧠 Today I worked on a classic number theory problem: checking whether a number is an Armstrong number. A number is an Armstrong number if the sum of its own digits each raised to the power of the number of digits is equal to the original number. For example: 153 has 3 digits 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 So 153 is an Armstrong number. What this function does: First, I store a copy of the original number so I can compare at the end. Then I find how many digits the number has using len(str(copy)). Inside the loop, I extract each digit, raise it to the power of the number of digits, and keep adding it to a running sum. Finally, I compare the sum with the original number: If they are equal, it’s an Armstrong number. Otherwise, it’s not. What I learned from this exercise: How to break a number down digit by digit using % 10 and // 10. How to combine math (powers) with loops and conditionals to implement a rule. The importance of keeping a copy of the original value when you are modifying it in a loop. Day 5 done ✅ 360 more to go. If you have any variations of this problem (like finding all Armstrong numbers in a range or handling user input with validation), share them—I’d love to try them out. #100DaysOfCode #365DaysOfCode #Python #LogicBuilding #ArmstrongNumber #NumberTheory #CodingJourney #LearnInPublic #AspiringDeveloper

  • text

To view or add a comment, sign in

Explore content categories