In the following example, we will check whether the number 19 is an Armstrong number or not.
Python Compiler
import math
num =19;
copyNum = num;
digits =0;
remainder =0;
total =0;
#find number of digits in num variablewhile(copyNum !=0):
digits +=1;
copyNum = math.floor(copyNum /10);
copyNum = num;
#slice the numbers from last digitswhile(copyNum !=0):
remainder = copyNum %10
total +=pow(remainder, digits)
copyNum = math.floor(copyNum /10)
#resultif(num == total):
print("%d is an armstrong number"% num);
else:
print("%d is not an armstrong number"% num);
Output
19 is not an armstrong number
Armstrong Numbers between the Given Range
In the following example, we will find all the Armstrong numbers between 1 and 200.
Example
Python Compiler
import math
start =1;
end =200;
flag =0;
print("Armstrong numbers between %d and %d: "% (start, end))
for start inrange(start, end+1):
#find number of digits in start variable
copyNum = start;
total =0;
digits =0;
remainder =0;
while(copyNum !=0):
digits +=1
copyNum = math.floor(copyNum /10)
copyNum = start;
#slice the numbers from last digitswhile(copyNum !=0):
remainder = copyNum %10;
total +=pow(remainder, digits);
copyNum = math.floor(copyNum /10);
if((start == total) and (start != 0)):
flag =1print(start, end=" ")
if(flag ==0):
print("There is no armstrong number between the given range")
Output
Armstrong numbers between 1 and 200:
1 2 3 4 5 6 7 8 9 153
Check Armstrong Number for any Given Number
In the following example, we will find whether the user entered number is an Armstrong number or not.
Example
Python Compiler
import math
num =int(input("Enter a (int) number: "));
copyNum = num;
digits =0;
remainder =0;
total =0;
#find number of digits in num variablewhile(copyNum !=0):
digits +=1;
copyNum = math.floor(copyNum /10);
copyNum = num;
#slice the numbers from last digitswhile(copyNum !=0):
remainder = copyNum %10
total +=pow(remainder, digits)
copyNum = math.floor(copyNum /10)
#resultif(num == total):
print("%d is an armstrong number"% num);
else:
print("%d is not an armstrong number"% num);
Output
Enter a (int) number: 25
25 is not an armstrong number
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on Python with examples for quick and easy learning.
We are working to cover every Single Concept in Python.