Program to Check Armstrong Number in python

Python Program to Check Armstrong Number

Armstrong number in python-In this article, we will see a Python program through which we can check if the given number is an Armstrong number or not.

Armstrong Number in python

What is Armstrong number in Python?

An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.

A positive integer is called an Armstrong number of order n if

abcd… = an + bn + cn + dn + …

In case of an Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

What is Armstrong Number example?

For example: 

371 is an Armstrong number since 3*3*3 + 7*7*7 + 1*1*1 = 371.

153 is an Armstrong number since 1*1*1 + 5*5*5 + 3*3*3 = 153.

 0, 1, 153, 370, 371 and 407 are the Armstrong numbers

Is zero an Armstrong number?

As you know an Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

For example, … So, the answer to your question is a little ambiguous, because some mathematicians say that 0 is an Armstrong number because 0 can also be written as 000.

Algorithm to Check Armstrong Number of 3 digits

The algorithm to check Armstrong number in Python are given below:

  • First Step: Take input from the user
  • Second Step: Find the cube of each digit of entered number
  • Third Step: Add the cube of all the digits
  • Fourth Step: If the output of step 3 is equal to the entered number i.e. Step 1. Then the print entered number is Armstrong number.
  • Fifth Step: If the output of step 3 is equal to the entered number i.e. Step 1. Then print entered number is not an Armstrong number.

Source Code: Check Armstrong number of 3 digits

# 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: 153
153 is an Armstrong number

Algorithm to Check Armstrong Number of n digits

The algorithm to check Armstrong number in Python are given below:
  • First Step: Take input from the user
  • Second Step: Count the Number of digits in a given number (For Example, 153 means 3).
  • Third Step: Find the power of n for each digit of the entered number.
  • Fourth Step: Add the nth power of all the digits
  • Fifth Step: If the output of step 4 is equal to the entered number i.e. Step 1. Then the print entered number is Armstrong number.
  • Sixth Step: If the output of step 4 is not equal to the entered number i.e. Step 1. Then print entered number is not an Armstrong number.
Source Code: Check Armstrong number of n digits
num = int(input("Please Enter the Number to Check for Armstrong: ")) 
# Changed num variable to string,
# and calculated the length (number of digits)
times = len(str(num))
# Initializing sum
sum = 0
# Finding Armstrong Number
temp=num
while temp > 0:
    digit = temp % 10
    sum += digit ** times
    temp //= 10
 
if num == sum:
    print(num," is Armstrong Number." )
else:
    print(num," is Not a Armstrong Number")
   
output:
Please Enter the Number to Check for Armstrong: 1634
1634 is Armstrong Number.
 
Please Enter the Number to Check for Armstrong: 1567
1567 is Not an Armstrong Number

Read the more interesting article: click here

Read the more interesting article in python: click here

Leave a Reply

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