Write a python program to check the given number is Armstrong or not usingwhile loop.
Question
Write a python program to check the given number is Armstrong or not using while loop.
Solution
Sure, here is a Python program that checks if a given number is an Armstrong number or not using a while loop:
# Function to check if a number is Armstrong
def is_armstrong(num):
# Initialize sum
sum = 0
# Find the length (order) of the number
order = len(str(num))
# Make a copy of num
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
# If the sum is equal to num, then it is an Armstrong number
if num == sum:
return True
else:
return False
# Test the function
num = int(input("Enter a number: "))
if is_armstrong(num):
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
This program works by first determining the number of digits in the number (the order). It then takes each digit of the number, raises it to the power of the order, and adds it to a sum. If the sum is equal to the original number, then the number is an Armstrong number.
Similar Questions
Complete the code segment to check whether the number is an Armstrong number or not.Armstrong Number:
Which of the following is an Armstrong number?(Armstrong number is a number that is equal to the sum of cubes of its digits.) 123 333 407 663
What are looping statements. Write a program to check whether a number is prime no. or not.
You are given a year in the form of an integer 'N', and your task is to check whether the given year is a leap year or not.
What is the output of the following?ct = 2while True: if ct % 0O6 == 0: break print(ct) ct += 1Options2345error.None of these2 3 4 5 6
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.