Python program to check the input integer number is an Armstrong number using the for loop and while loop
ADVERTISEMENTS
In this article, you will learn how to check the input integer number is an Armstrong number using the for loop and while loop in python programming.
Examples
Input: 153
153 is an Armstrong number
Input: 154
154 is not an Armstrong number
Armstrong Number
A positive integer is called an Armstrong number (of order n) if
pqrs ... nth = pn + qn + rn + ... + nth
You should have knowledge of the following topics in python programming to understand these programs:
- Python
input()
function - Python
str()
function - Python
int()
function - Python
for
loop statement - Python
while
loop statement - Python
If-else
condition statement - Python
print()
function
1. Source Code using the for loop
# Python program to check the input integer number is an Armstrong number using the for loop
print("Enter the positive integer number:", end=" ")
x, r = str (input ()), 0
for i in x:
r += int (i) ** len (x)
# if x is equal to r, the number is an Armstrong number
if int (r) == int (x):
print(x, " is an Armstrong number.")
else:
print(x, " is not an Armstrong number!")
Output
Enter the positive integer number: 153
153 is an Armstrong number.
2. Source Code using the while loop
# Python program to check the input integer number is an Armstrong number using the while loop
print("Enter the positive integer number:", end=" ")
x, r, i = str (input ()), 0, 0
while i < len (x):
r += int (x[i]) ** len (x)
i += 1
# if x is equal to r, the number is an Armstrong number
if int (r) == int (x):
print(x, " is an Armstrong number.")
else:
print(x, " is not an Armstrong number!")
Output
Enter the positive integer number: 370
370 is an Armstrong number.
Explanation
In these given programs, we have taken input 153
a positive integer number to check it is an Armstrong or not using the for
loop and while
loop statement.
We applied the Armstrong number formula in these loops to check the sum of its digits' multiplication with its length is equal to the given input value.