Python Program to find Armstrong number in an Interval

Python Program to find Armstrong number in an Interval

An Armstrong number is a number whose sum of the nth power of the individual digits in a given number is equal to that particular number. for example, let us take a number 153 as the sum of the power of the individual digits equals 153 so its an Armstrong number. single numbers are trivially Armstrong numbers and there are 88 almost Armstrong numbers in total with base 3 that is the sum of cubes of the individual digits of a particular number.

for i in range(1001):  
  num=i

  result=0

  n = len(str(i))

  while(i!=0):

    digit=i%10

    result=result+digit**n

    i=i//10

    if(num==result):

      print(num)

 

STEP 1: In this step, we use for loop to iterate the numbers from 1 to 1000

STEP 2: In this step, we initialize the number to i and the result to 0

STEP 3: In this step, we assign the length of the number to a variable n

STEP 4: In this step, using the while loop we give a certain condition.

STEP 5: In this step, we will find the remainder using the modulus operator and get a digit

STEP 6: In this step, we will cube the number and add it to the result.

STEP 7: In this step, the number is operated using the floor division and the numbers after the decimal point are removed in this floor division operation.

STEP 8: In this step, if the number equals the result then we will print the armstrong numbers in the range of 1 to 1000.

OUTPUT:

1
2
3
4
5
6
7
8
9
25
36
125
153
216
370
371
407
729

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *