Write a Python Program to Check Armstrong Number

Python Program to Check Armstrong Number

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn  Python programming concepts. You can learn Python language at your own speed and time. One can learn concepts of Python language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Python Program to Check Armstrong Number
Python Program to Check Armstrong Number

Write a Python Program to Check Armstrong Number

PROGRAM: Python Program to Check Armstrong Number

/* Python Program to Check Armstrong Number */

# Python program to check if the number is an Armstrong number or not

# take input from the user
num = int(input(“Enter a number: “))

# initialize sum
sum = 0

# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

# display the result
if num == sum:
print(num,”is an Armstrong number”)
else:
print(num,”is not an Armstrong number”)

OUTPUT :-

Output 1

Enter a number: 663
663 is not an Armstrong number

Output 2

Enter a number: 407
407 is an Armstrong number

 2.Check Armstrong number of n digits

num = 1634

# Changed num variable to string,
# and calculated the length (number of digits)
order = len(str(num))

# initialize sum
sum = 0

# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10

# display the result
if num == sum:
print(num,”is an Armstrong number”)
else:
print(num,”is not an Armstrong number”)

Leave a Reply

Your email address will not be published. Required fields are marked *