Python Program to Check Armstrong Number

In this python tutorial, you will learn how to Check Armstrong Number using the if and else statement and while loop along with the different operators of the python programming language.

Armstrong number is a number that is equal to the sum of cubes of its digits. 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.

Eg: 153 = ((1*1*1 )+ (5*5*5) + (3*3*3)) = 153

How to Check Armstrong Number?

Let’s take a look at the source code , here the values are given as input by the user in the code, the if, else statement and while loop along with the assignment operators carry out the function.

CASE 1:

RUN CODE SNIPPET

CASE 2:

RUN CODE SNIPPET
#Python Program to Check Armstrong Number
num = int(input("Enter the first integer: ")) 

order = len(str(num))

sum = 0

temp = num
while temp > 0:
   digit = temp % 10
   sum += digit ** order
   temp //= 10

if num == sum:
   print("\n", num,"is an Armstrong number")
else:
   print("\n", num,"is not an Armstrong number")

CASE 1:

INPUT:

153

OUTPUT:

Enter the first integer: 
153 is an Armstrong number

CASE 2:

INPUT:

382

OUTPUT:

Enter the first integer: 
382 is not an Armstrong number

  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable num with the statements/strings ("Enter the first integer: "), we use the int function and declare the  input value as an integer value.
  2. In the STDIN section of the code editor the input values are entered.
  3. We declare the order and assign the condition len(str(num)),  where the condition is the length of the value in string format which the variable order holds .
  4. Next, we declare the that sum = 0, followed by which we declare temp = num.
  5. We open a while loop with the condition temp > 0 followed by a :, after which we declare the condition digit = temp % 10 and the condition sum += digit ** order where temp //= 10 .
  6. In the above lines of code, the while loop will iterate until all the functions digit = temp % 10, sum += digit ** order and temp //= 10 are complete and the conditions are satisfied. That is we find the modulus % of temp where the value of digit obtained is used in the next condition, these steps repeat until the condition temp //= 10 is satisfied and the loop comes to an end.
  7. After the while loop, we declare an if  statement where if the condition num == sum is satisfied, its displays the statement ("\n", num,"is an Armstrong number") using the print function.
  8. If the condition is not satisfied, it moves to the next step which is the else  statement where the print function will display the statement ("\n", num,"is an Armstrong number").

NOTE:

  • The len() function is an inbuilt function in Python programming language that returns the length of the string.
  • The While true loop in python runs without any conditions until the break statement executes inside the loop.
  • The double division operator (//) is used to compute the quotient and the modulus operator (%) is used to compute the reminder.
  • The Python += operator lets you add two values together and assign the resultant value to a variable
  • The print statement/string to be displayed in enclosed in double quotes.
  •  The integers are zero, positive or negative whole numbers without a fractional part.
  • The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
  • The == equality operator is a comparison operator which returns True is the two items are equal and returns False if not equal.
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.
  • The print statement is followed by a period, to initiate the format function.
  • The print statement/string to be displayed in enclosed in double quotes.

Leave A Reply

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

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...