Aryan PrajapatKnowledge Contributor
Write a program to check if the given number is Armstrong or not in python.
Write a program to check if the given number is Armstrong or not in python.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
def is_armstrong(num):
# Calculate the number of digits in the number
num_digits = len(str(num))
# Initialize sum to store the result
sum = 0
# Temporary variable to store the original number
temp = num
# Calculate Armstrong sum
while temp > 0:
digit = temp % 10
sum += digit ** num_digits
temp //= 10
# Check if the number is Armstrong or not
if num == sum:
return True
else:
return False
# Input from the user
number = int(input(“Enter a number: “))
# Check if the number is Armstrong or not
if is_armstrong(number):
print(f”{number} is an Armstrong number.”)
else:
print(f”{number} is not an Armstrong number.”)