;

Python Program to Check whether the Number is Armstrong Number or Not


Tutorialsrack 22/04/2020 Python

In the Python program, we will learn how to check whether the number is Armstrong number or not.

What is an Armstrong number?

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. 

For Example,  153 is an Armstrong number.

      (1)3 + (5)3 + (3)3= 153.

Here is the code of the program to check if the given number is Armstrong Number or Not.

Program 1: Check 3 Digit Number is Armstrong or Not

Program 1: Check 3 Digit Number is Armstrong or Not
# Python Program to Check whether the Number is 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

Enter a number: 5

5 is not an Armstrong number

 

Enter a number: 153

153 is an Armstrong number

Program 2: Check nth Digit Number is Armstrong or Not

Program 2: Check nth Digit Number is Armstrong or Not
# Python Program to Check whether the Number is Armstrong Number or Not
# Take input from the user
num = int(input("Enter a number: "))
 
# 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")
Output

Enter a number: 153

153 is an Armstrong number

 

Enter a number: 1634

1634 is an Armstrong number


Related Posts



Comments

Recent Posts
Tags