Robotics & Artificial Intelligence
Write a function which returns if a number passed as as argument is a perfect number or an Armstrong number.
Answer
def check_number(n):
s = 0
temp = n
digits = len(str(n))
while temp > 0:
d = temp % 10
s = s + d ** digits
temp = temp // 10
p = 0
for i in range(1, n):
if n % i == 0:
p = p + i
if p == n:
return "Perfect Number"
elif s == n:
return "Armstrong Number"
else:
return "Neither Perfect nor Armstrong"
num = int(input("Enter a number: "))
result = check_number(num)
print(result)Output
Enter a number: 9474
Armstrong Number
Enter a number: 28
Perfect Number
Related Questions
Distinguish between void functions and non-void functions with the help of an example.
Discuss the structure of a Python function with the help of an example.
Write a function which returns if a number passed as an argument is a prime or composite number.
Write a function which counts and displays the number of vowels, consonants, uppercase, and lowercase characters in a string entered as input.