Please login


Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python Program Armstrong Numbers Between Two Intervals

Find Armstrong Numbers Between Two Intervals

This python program will identify the Armstrong number between two intervals , the user is required to insert integer numbers. As we know about what are Armstrong Numbers? A Armstrong number is one which have the sum of its digits raised to their cube equal to itself. In this program, User have to enter two number such as start and end number. Then, we will write some conditions through which we will find how to find Armstrong Numbers.
For example:
13+53+33 = 153

Basically we know that Armstrong number in given range 150 to 160  is only 153.  All other numbers are not Armstrong Number.
Python Program Armstrong Numbers Between Two Intervals


Step 1. Start

Step 2. Import math class.

Step 3. Take two user input and store into int type value variable such as first and second.

Step 4. Write a function with name is_Armstrong(int value).

Step 5. Inside the function, Initialize the sum variable to 0.

Step 6. First Convert int type value to string type and then convert string value into array format. and store into arr variable.

Step 7. Run a for loop starts from 0 to length of the array.

Step 8. Inside for loop, using power method, and calculate the power of each element to its array size. Then add one by one with sum variable and stored into the sum variable as well.

Step 9. Outside the for loop, compare the values of sum and value, if both values are equal then print, “Given number is Armstrong number”.

Step 10. Else, print, “Given number is not Armstrong Number”.

Stop 11. Outside the function, write a for loop which starts from first to second+1 value.

Step 12. Inside for loop, call the is_Armstrong function and pass the loop value to the function.

Stop 10. Stop

Python Program:

import math

first = int(input("Enter first number:"))
second = int(input("Enter second number:"))

def is_Armstrong(val: int) -> bool:
    sum = 0
    arr = [int(d) for d in str(val)]
    for i in range(0, len(arr)):
        sum = sum + math.pow(arr[i], len(arr))
    if sum == val:
        print("{} number is Armstrong".format(val))
        print("{} number is not Armstrong".format(val))

for i in range(first, second + 1):
# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student) 


Enter first number:150
Enter second number:160
150 number is not Armstrong
151 number is not Armstrong
152 number is not Armstrong
153 number is Armstrong
154 number is not Armstrong
155 number is not Armstrong
156 number is not Armstrong
157 number is not Armstrong
158 number is not Armstrong
159 number is not Armstrong
160 number is not Armstrong