Python Program to Generate Armstrong Numbers

This article is created to cover some programs in Python, that generates Armstrong numbers. Here are the list of programs:

Before creating these programs, let's remind about Armstrong numbers.

What are Armstrong Numbers ?

Any number that equals to sum of its own digits, where each digit raised to the power of number of digits. For example, 1634 is an Armstrong number. because:

14 + 64 + 34 + 44
= 1 + 1296 + 81 + 256
= 1297 + 337
= 1634

Note - Since 1634 is a 4-digit number, therefore each of its digit are raised to the power of 4

Generate and Print Armstrong Numbers from 1 to 1000

To generate Armstrong numbers in python, you have to ask from user to enter the interval (starting and ending number), then generate and print Armstrong numbers between given range like shown in the program given below.

The question is, write a Python program to generate Armstrong numbers from 1 to 1000. Here is its answer:

print("Armstrong Numbers Between 1 and 1000:")
first = 1
last = 1000
while first<=last:
  res = 0
  temp = first
  noOfDigit = 0
  while temp>0:
    temp = int(temp/10)
    noOfDigit = noOfDigit + 1
  num = first
  while num>0:
    rem = num%10
    pow = 1
    i = 0
    while i<noOfDigit:
      pow = pow*rem
      i = i+1
    res = res+pow
    num = int(num/10)
  if res == first:
    print(res)
  first = first+1

The snapshot given below shows the sample output produced by this Python program:

generate armstrong numbers python

The dry run of above program goes like:

Generate Armstrong Numbers in Given Range

This program receives interval (range) from user to generate and print Armstrong numbers between given range.

print("Enter Range (Interval): ")
first = int(input())
last = int(input())

if first>last:
  temp = first
  first = last
  last = temp

while first<=last:
  res = 0
  temp = first
  noOfDigit = 0
  while temp>0:
    temp = int(temp/10)
    noOfDigit = noOfDigit + 1
  num = first
  while num>0:
    rem = num%10
    pow = 1
    i = 0
    while i<noOfDigit:
      pow = pow*rem
      i = i+1
    res = res+pow
    num = int(num/10)
  if res == first:
    print(res)
  first = first+1

Here is its sample run:

print armstrong number python

Now supply the input say 200 and 10000 as range or interval to find and print all Armstrong numbers between this range like shown in the snapshot given below:

python generate armstrong numbers

Modified Version of Previous Program

This is the modified version of previous program, uses try-except to handle with invalid inputs. The str() converts any type of value to a string type. The len() used in this program to find length of string (indirectly used to calculate number of digits):

print("Enter Range (Interval): ", end="")
try:
  first = int(input())
  try:
    last = int(input())
    if first>last:
      temp = first
      first = last
      last = temp
    print()
    while first<=last:
      res = 0
      noOfDigit = len(str(first))
      num = first
      while num>0:
        rem = num%10
        res = res + (rem ** noOfDigit)
        num = int(num/10)
      if res == first:
        print(res, end=" ")
      first = first+1
    print()
  except ValueError:
    print("\nInvalid Input!")
except ValueError:
  print("\nInvalid Input!")

Here is its sample run with range input as 100 and 5000:

python generate armstrong numbers in given range

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »