Find the Armstrong Numbers between Two Intervals using Python

Find the Armstrong Number in a given Range in Python

Given two integers high and low for limits as inputs, the objective is to write a code to Find the Armstrong Numbers in a given Interval in C++. 

For Instance,
Input : 150 160
Output : 153
Armstrong Numbers Between Two Intervals in python

Find the Armstrong Numbers in a given Range in Python

Given two integer inputs as intervals high and low, the objective is to write a python code to check if the numbers lying within the given interval are Armstrong Numbers or not.

An Armstrong number or a Narcissistic number is any number that sums up itself when each of its digits is raised to the power of a total number of digits in the number. Let us try to understand this through the below example,

  • abcd… = an + bn + cn + dn + …
  • Where n is the order(length/digits in number)

Let’s look at some examples of Armstrong Numbers.

Examples

Here are few examples that’ll help you to understand the concept better.

Find the Armstrong Number in a given Interval in Python

Method 1

Python Code

Run
low, high = 10, 10000

for n in range(low, high + 1):

    # order of number
    order = len(str(n))

    # initialize sum
    sum = 0

    temp = n
    while temp > 0:
        digit = temp % 10
        sum += digit ** order
        temp //= 10

    if n == sum:
        print(n, end=", ")

Method 2

Python Code

Run
import math

first, second = 150, 10000


def is_Armstrong(val):
    sum = 0

    # this splits the val into its digits
    # example val : 153 will become [1, 5, 3]
    arr = [int(d) for d in str(val)]

    # now we iterate on array items (digits)
    # add these (digits raised to power of len i.e order) to sum
    for i in range(0, len(arr)):
        sum = sum + math.pow(arr[i], len(arr))

    # if sum == val then its armstrong
    if sum == val:
        print(str(val) + ", ", end="")


for i in range(first, second + 1):
    is_Armstrong(i)

Output

153, 370, 371, 407, 1634, 8208, 9474, 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

15 comments on “Find the Armstrong Numbers between Two Intervals using Python”


  • Prashant

    first=int(input(“Enter the first number:=”))
    end=int(input(“Enter the last number:=”))
    def arm(first,end):
    result=[]
    for i in range(first,end+1):
    sum=0
    val=[int(d) for d in str(i)]
    n=len(val)
    for j in val:
    sum+=j**n
    if i==sum:
    result.append(sum)
    print(result)

    arm(first,end)


  • Harshith

    start = int(input(‘Enter starting number: ‘))
    end = int(input(‘Enter ending number: ‘))
    print(‘Amstrong between {} and {} are ‘.format(start,end))
    for i in range(start,end):
    num = [int(a) for a in str(i)]
    sum=0
    for k in range(len(num)):
    sum = sum + pow(num[k],len(num))
    if sum==i:
    print(sum,end=’ ‘)


  • Bibhudutta

    a=int(input(‘Enter first interval’))
    b=int(input(‘Enter Ending interval’))

    for i in range(a,b+1):
    sum = 0
    temp=i
    order = len(str(i))
    while(i>0):
    rem=i%10
    sum=sum+rem**order
    i=i//10

    if(temp==sum):
    print(temp,’ number is Armstrong’)
    else:
    print(temp,’ number is not Armstrong’)

    output
    ———————
    Enter first interval150
    Enter Ending interval160
    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


  • mohammadnawaz111

    n=int(input(“Enter first number : “))
    m=int(input(“Enter second number : “))
    for num in range(n, m + 1):
    order = len(str(num))
    sum = 0
    temp = num
    while temp > 0:
    digit = temp % 10
    sum += digit ** order
    temp //= 10
    if num == sum:
    print(num,”is Armstrong Number”)
    else:
    print(num,”is not an Armstrong number”)


  • Abhiroop

    num = str(input())
    lst = [(int(i)) ** 3 for i in num]
    print(True) if sum(lst) == int(num) else print(False)


  • sandesh

    a=int(input())
    b=int(input())
    for i in range(a,b+1):
    a1=str(i)
    n=len(a1)
    sum=0
    for j in a1:
    sum=sum+int(j)**n
    if sum==int(i):
    print(i)


  • Priya

    n=int(input(“enter 1st no”))
    m=int(input(“enter 2nd no”))
    for x in range(n,m+1):
    temp=x
    sum=0
    while temp>0:
    r=temp%10
    sum=sum+r**3
    temp=temp//10
    if sum==x:
    print(“armstrong no”,sum)


  • satya

    start=int(input(“enter a start no:”))
    end=int(input(“enter a end no:”))
    for n in range(start,end+1):
    sum=0
    num=n
    while(num>0):
    rem=num%10
    sum=sum+(rem**3)
    num=num//10
    if(n==sum):
    print(n)


    • satya

      start=int(input(“enter a start no:”))
      end=int(input(“enter a end no:”))
      for n in range(start,end+1):
      order=len(str(n))
      sum=0
      num=n
      while(num>0):
      rem=num%10
      sum=sum+(rem**order)
      num=num//10
      if(n==sum):
      print(n)


  • ROHAN

    n = input().split()
    if n[0] == 0:
    n[0] = 1

    total = 0
    for j in range(int(n[0]),int(n[1])+1):
    total = 0
    for i in str(j):
    total += int(i)**3

    if total == j:
    print(j)


  • gaurav

    import math

    def armstrong_no(n):
    k = len(n)
    sum = 0
    for i in n:
    sum+= pow(int(i), k)

    if sum == int(n):
    return 1
    else:
    return 0

    a = int(input(“first number: “))
    b = int(input(“second number: “))
    c = []
    for j in range(a, b+1):
    j = str(j)
    if armstrong_no(j) == 1:
    c.append(j)
    print(“armstrong numbers: ” + str(c))


  • Pavithra

    n=input(“Enter first number:”)
    x=input(“Enter second number:”)
    z=0
    count=0
    for i in range(int(n),int(x)+1,1):
    k=str(i)
    d=len(k)
    h=[l for l in k]
    for c in h:
    z=z+(int(c)**d)
    if(z == int(i)):
    print(i,”number is Armstrong”)
    count=count+1
    z=0
    h.clear()
    else:
    print(i,”number is not Armstrong”)
    z=0
    h.clear()
    print(count)


  • Abhishek

    n1 = int(input(“Enter Lower range : “))
    n2 = int(input(“Enter Upper range : “))

    for i in range(n1, n2+1):

    ord = len(str(i))

    sum = 0

    temp = i

    while temp > 0:
    digit = temp % 10
    sum = sum + digit ** ord
    temp = temp // 10
    if i == sum:
    print(i)