added method for checking armstrong number (#1936)

* added method for checking armstrong number

* Update comment

Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Akash 2020-05-03 23:26:33 +05:30 committed by GitHub
parent b6fcee3114
commit f80ffe1f54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,6 +41,14 @@ def armstrong_number(n: int) -> bool:
temp //= 10
return n == sum
def narcissistic_number(n:int) -> bool:
"""Return True if n is a narcissistic number or False if it is not"""
expo = len(str(n)) #power, all number will be raised to
temp = [(int(i)**expo) for i in str(n)] # each digit will be multiplied expo times
# check if sum of cube of each digit is equal to number
return n == sum(temp)
def main():
"""
@ -48,6 +56,7 @@ def main():
"""
num = int(input("Enter an integer to see if it is an Armstrong number: ").strip())
print(f"{num} is {'' if armstrong_number(num) else 'not '}an Armstrong number.")
print(f"{num} is {'' if narcissistic_number(num) else 'not '}an Armstrong number.")
if __name__ == "__main__":