Signup/Sign In
PUBLISHED ON: JULY 7, 2021

Python Program to Check Armstrong Number

In this tutorial, we will learn how to check whether a positive integer entered by the user is Armstrong Number or not using Python programing language. Before moving further it is advisable that you should have knowledge about conditional statements in Python. Check out this tutorial on Conditional Statements in Python.

A positive integer having n digits is Armstrong if, the sum of the digits raised to the power n is equal to the number.

For a 3 digit number, we can say that a number is an Armstrong if the sum of the cubes of a number is equal to the number itself.

Suppose the number is 153 then,

1³ + 5³ +3³ = 1 + 125 + 27 = 153

Since the sum of cubes is 153 the number is Armstrong number.

The program should take input as follows and return the following as output.

Input- Enter Number: 153

Output- 153 is an Armstrong number

To execute this task, we should follow the same logic of extracting digits by dividing the number by 10 till it becomes 0. Check the tutorial on finding sum of digits using an iterative approach where the process of finding digits is discussed in detail.

Let us look at the algorithm to understand the working of the code clearly,

Algorithm

Step 1- Define a function Armstrong() which will accept a number and its order as parameters

Step 2- Initialise sum to 0

Step 3- Declare a temporary variable temp to store a copy of the number

Step 4- Run a loop till temp is greater than 0

Step 5- Declare a variable digit to store and calculate the digit of a number by dividing it by 10

Step 6- Declare a variable sum which will store the sum of the digits raised to the power of the order of the digit

Step 7- Update temp to n//10

Step 8- Check whether the sum is equal to the number

Step 9- If true, print it is an Armstrong number

Step 10- Else print not an Armstrong number

Python Program

Look at the program to understand the implementation of the above-mentioned approach.

def Armstrong(n,o):
    sum = 0
    temp = n
    while temp > 0:
       digit = temp % 10
       sum += digit ** o
       temp = temp//10
    if n == sum:
       print(n,"is an Armstrong number")
    else:
       print(n,"is not an Armstrong number")
    
num = int(input("Enter Number: "))
order = len(str(num))
Armstrong(num,order)


Enter Number: 1634
1634 is an Armstrong number

Enter Number: 456234
456234 is not an Armstrong number

Conclusion

In this tutorial, we learned how to do a program to check if a number is Armstrong or not in Python. First, we defined a function that checks for Armstrong number and displays the output accordingly. The function will have the number and its order as parameters. For finding the order (the number of digits) of the number we used str() and len() functions that are predefined in the string class of Python. The function then checks using if-else conditional statements and prints the result.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.