How to generate armstrong numbers in Python?


Any three digit number is called an Armstrong number of sum of cube of its digits equals the number itself. In order to check if a number satisfies this condition, each digit from it is successively separated from right and its cube is cumulatively added. In the end if the sum is found to be equal to original number, it is called Armstrong number.

Example

Following Python code prints all armstrong numbers between 100 to 999

for num in range(100,1000):
  temp=num
  sum=0
  while temp>0:
    digit=temp%10
    sum=sum+digit**3
    temp=temp//10

  if sum==num:
    print (num)

Output

The output is list of armstrong numbers

153
370
371
407

Updated on: 21-Feb-2020

886 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements