How to Check Armstrong Number in Python

In this tutorial, we will create a "How to Check Armstrong Number in Python". The purpose of this tutorial is to help you learn to code the Armstrong number checker. We will take a deep look into the code and understand the structure of the program. I will provide a sample program to demonstrate the actual coding of this tutorial.

This tutorial is simple and easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program will tell you whether the number you entered is an Armstrong number. I will do my best to provide you with the most simplified method of creating this program, called "Armstrong Number Checker". So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display the armstrong number checker. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. while True:
  2.    print("\n\n========== How to Check Armstrong Number in Python ========== \n\n\n")
  3.    ret=False
  4.    num=int(input("Enter a number: "))
  5.  
  6.    order = len(str(num))
  7.  
  8.  
  9.    sum = 0
  10.  
  11.  
  12.    temp = num
  13.    while temp > 0:
  14.       digit = temp % 10
  15.       sum += digit ** order
  16.       temp //= 10
  17.  
  18.  
  19.    if num == sum:
  20.       print(num,"is an Armstrong number")
  21.    else:
  22.       print(num,"is not an Armstrong number")
  23.      
  24.    user_input=input("\n\nTry again? (Yes/No): ")
  25.    print("\n\n")
  26.    if user_input.lower()=='no':
  27.       print("Exit program.")
  28.       break
  29.  
  30.    elif user_input.lower()=='yes':
  31.       ret=True
  32.  
  33.    else:
  34.       print("Wrong input.")
  35.       ret=True
  36.  
  37.  
  38.    if ret:
  39.       continue  

In the code that I provided, you will see that I enclosed the entire code with a while loop to ensure we can loop around its structure. Next, we set multiple variables to handle the values we will be using.

Following that, we use another while loop to check if the number is zero, and after that, we incorporate several lines of code to calculate the Armstrong number.

Output:

The How to Check Armstrong Number in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Check Armstrong Number in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment