The below function is written to check whether a given three digit number is an Armstrong number.
Hint: An “Armstrong number” is an n-digit number that is equal to the sum of the nth powers of its individual digits.
Example: 371 is an Armstrong number as 371 = 3^3 +7^3+ 1^3
Source:
Hint: An “Armstrong number” is an n-digit number that is equal to the sum of the nth powers of its individual digits.
Example: 371 is an Armstrong number as 371 = 3^3 +7^3+ 1^3
Source:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Created on 24-Aug-2019 | |
@author: Anonymous | |
''' | |
#PF-Tryout | |
def find_armstrong(number): | |
temp=0 | |
x=number # | |
while(number!=0): | |
remainder=number%10 | |
# This Statement changing the value of number variable so | |
number=number//10 | |
# For keeping original Number we took it in a variable named a "x" | |
temp+=(remainder*remainder*remainder) | |
if(x==temp): | |
return True | |
return False | |
number=371 | |
if(find_armstrong(number)): | |
print(number,"is an Armstrong number") | |
else: | |
print(number,"is not an Armstrong number") |
No comments:
Post a Comment