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 SNIPPETCASE 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
- 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 variablenum
with the statements/strings("Enter the first integer: ")
, we use theint
function and declare the input value as an integer value. - In the STDIN section of the code editor the input values are entered.
- We declare the
order
and assign the conditionlen(str(num))
, where the condition is the length of the value in string format which the variableorder
holds . - Next, we declare the that
sum = 0
, followed by which we declaretemp = num
. - We open a
while
loop with the conditiontemp > 0
followed by a:
, after which we declare the conditiondigit = temp % 10
and the conditionsum += digit ** order
wheretemp //= 10
. - In the above lines of code, the
while
loop will iterate until all the functionsdigit = temp % 10
,sum += digit ** order
andtemp //= 10
are complete and the conditions are satisfied. That is we find the modulus % oftemp
where the value ofdigit
obtained is used in the next condition, these steps repeat until the conditiontemp //= 10
is satisfied and the loop comes to an end. - After the
while
loop, we declare anif
statement where if the conditionnum == sum
is satisfied, its displays the statement("\n", num,"is an Armstrong number")
using theprint
function. - If the condition is not satisfied, it moves to the next step which is the
else
statement where theprint
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.