Armstrong Number:
Beginners sometimes ask what the Armstrong number, also known as the narcissist number, is. Because of the way the number behaves in a given number base, it is particularly interesting to new programmers and those learning a new programming language. The Armstrong number meaning in numerical number theory is the number in any given number base that forms the sum of the same number when each of its digits is raised to the power of the number’s digits.
Ex: 153, 371 etc.
Example1:
Input:
number =153
Output:
153 is Armstrong number
Explanation:
Here 1^3 + 5^3 + 3^3 = 153 so it is Armstrong Number
Example2:
Input:
number =79
Output:
79 is not Armstrong number
Explanation:
Here 7^2 + 9^2 = 130 not equal to 79 so it is not Armstrong Number
Armstrong Number in Python
Below are the ways to check Armstrong number in python
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
Method #1: Using while loop
- Count how many digits there are in the number.
- Using mod and division operations, each digit is accessed one after the other.
- Each digit is multiplied by the number of digits, and the result is saved in a separate variable.
- Steps 2 and 3 are repeated until all of the digits have been used.
- Analyze the result obtained using the original number.
- If both are same/equal then it is armstrong number
- Else it is not armstrong number.
Below is the implementation:
# given number num = 153 # intialize result to zero(ans) ans = 0 # calculating the total digits in the given number digits = len(str(num)) # copy the number in another variable(duplicate) dup_number = num while (dup_number != 0): # getting the last digit remainder = dup_number % 10 # multiply the result by a digit raised to the power of the number of digits. ans = ans + remainder**digits dup_number = dup_number//10 # It is Armstrong number if it is equal to original number if(num == ans): print(num, "is Armstrong number") else: print(num, "is not Armstrong number")
Output:
153 is Armstrong number
Method #2: By converting the number to string and Traversing the string to extract the digits
Algorithm:
- Initialize a variable say ans to 0
- Using a new variable, we must convert the given number to a string.
- Calculate the digits of the given number.
- Iterate through the string, convert each character to an integer, multiply the ans by a digit raised to the power of the number of digits of the given number.
- If the ans is equal to given number then it is Armstrong number
Below is the implementation:
# given number num = 153 # intialize result to zero(ans) ans = 0 # converting given number to string numString = str(num) # calculating the number of digits of the given number digits = len(numString) # Traverse through the string for char in numString: # Converting the character of string to integer # multiply the ans by a digit raised to the power of digits. ans = ans+int(char)**digits # It is Armstrong number if it is equal to original number if(num == ans): print(num, "is Armstrong number") else: print(num, "is not Armstrong number")
Output:
153 is Armstrong number
Related Programs: