In this number python program, we have to print Armstrong numbers in a range. It's a beginner-level python program.
To understand this example, you should have knowledge of the following Python programming topics:
We have already discussed what you mean by Armstrong number, A number equal to the sum of the cube of all digits in that number. We discussed it in checking Armstrong's number python program; please refer to it for details.
Now in this python program example, we need to print all the Armstrong numbers between the user given range, so we accept the range from the user and save that in two variables low and up. Now open a
for loop structure in python from the lower range to the upper range using
range function. Then inside that
for loop initialize the variable sum and keep the copy of the number in a temp variable. We take each number from the given range, and with the help of
while loop, we check for each number, which is Armstrong or not using the Mod operator. if it Armstrong, print that number and move to the next number in the
STEP 1: Accept two numbers from the user for lower and upper range using the input function in python programming language and convert it to int and save.
STEP 2: Open a
for loop using the range method from lower to the upper range to check every number for Armstrong or not. [note: in range function in python we need to use upper +1 to reach the upper limit]
STEP 3: Initialize the sum as zero and store the number in a temporary variable, it is used to check if the temp and sum are the same.
STEP 4: Open
while loop if the temp is greater than zero to check the number is Armstrong or not.
STEP 5: Now we split one digit from the number using the mod operator
STEP 6: Calculate the sum by taking the sum + cube of the digit
STEP 7: Divide the number with 10 to remove one digit to take the next digit.
STEP 8: Open an if condition to check the num is equal to the sum if so print the num.
low = int(input("Enter low range: ")) up = int(input("Enter up range: ")) for num in range(low, up + 1): sum = 0 temp = num while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 if num == sum: print(num)
Enter low range: 100 Enter up range: 200 153