
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Armstrong Number in Python
Suppose we have a k-digit number N. The N is an Armstrong number when the k-th power of each digit sums to N. So we have to return true if it is an Armstrong number, otherwise false.
To solve this, we will follow these steps −
- power := number of digits
- temp := n, res = 0
- while temp is not 0
- res := res + (temp mod 10) ^ power
- temp := temp / 10 //integer division
- if res = n, return true, otherwise false.
Example
Let us see the following implementation to get a better understanding −
import math class Solution(object): def poww(self,base,power): res = 1 while power: if power & 1: res *= base base *= base power>>=1 return res def isArmstrong(self, n): power =int(math.log10(n)) + 1 temp = n res = 0 while temp: res += (self.poww(temp%10,power)) temp//=10 return res == n ob1 = Solution() print(ob1.isArmstrong(153))
Input
153
Output
true
- Related Articles
- Python Program to Check Armstrong Number
- Armstrong number in Java.
- How to Find Armstrong Number in an Interval using Python?
- Armstrong number within a range in JavaScript
- C++ Program to Check Armstrong Number
- C Program to Check Armstrong Number?
- Java Program to Check Armstrong Number
- Swift Program to Check Armstrong Number
- Haskell Program to Check Armstrong Number
- How to generate armstrong numbers in Python?
- Golang Program to Check For Armstrong Number
- How to Check Armstrong Number between Two Integers in Golang?
- C++ Program to Display Armstrong Number Between Two Intervals
- Check the number is Armstrong or not using C
- Java Program to Check Armstrong Number between Two Integers

Advertisements