Write A Python Program To Print All Armstrong Numbers Between Two Given Numbers

Hi, in this blog let's learn two programs of Armstrong Number in Python.

  1. Check if Number is Armstrong Number
  2. Generate Armstrong Number in range


Armstrong Number is a number whose some of its individual digits raised to power of its length equals the number itself.

 

Armstrong number in python

 



Let's take two numbers as example

number = 548834

Length of number = 6

5 ^ 6 + 4 ^ 6 + 8 ^ 6 + 8 ^ 6 + 3 ^ 6 + 4 ^ 6 = 548834

number = 12345

Length of number = 5

1 ^ 5 + 2 ^ 5 + 3 ^ 5 + 4 ^ 5 + 5 ^ 5 = 4424




Logic of Armstrong Number

Take a variable sum_of_powers and set it to 0. Use a while loop to iterate until the number becomes zero, get the last digit of the number by modulating the number by 10.

Raise the last digit to the power of length of number and add it to the sum_of_powers.

Trim the last digit of the number using true division

    sum_of_powers = 0
    while temp != 0:
        last_digit = temp % 10
        sum_of_powers = sum_of_powers + last_digit ** length
        temp = temp // 10





Python Program to Generate Armstrong Number in Range

Use the for loop to iterate from range 1 to 100000000. Get the number length and use the while loop to get the last_digit raised to power of length. Check if sum_of_powers is equal to number. If yes print the Armstrong number else not an Armstrong number.

for number in range(1, 100000000):
    length = len(str(number))
    temp = number
    sum_of_powers = 0
    while temp != 0:
        last_digit = temp % 10
        sum_of_powers = sum_of_powers + last_digit ** length
        temp = temp // 10
    if sum_of_powers == number:
        print("Armstrong number is : ", number)




OUTPUT:

 
Armstrong number is :  1
Armstrong number is :  2
Armstrong number is :  3
Armstrong number is :  4
Armstrong number is :  5
Armstrong number is :  6
Armstrong number is :  7
Armstrong number is :  8
Armstrong number is :  9
Armstrong number is :  153
Armstrong number is :  370
Armstrong number is :  371
Armstrong number is :  407
Armstrong number is :  1634
Armstrong number is :  8208
Armstrong number is :  9474
Armstrong number is :  54748
Armstrong number is :  92727
Armstrong number is :  93084
Armstrong number is :  548834
Armstrong number is :  1741725
Armstrong number is :  4210818
Armstrong number is :  9800817
Armstrong number is :  9926315
Armstrong number is :  24678050
Armstrong number is :  24678051
Armstrong number is :  88593477



Python Program to Check if Number is Armstrong Number.

Ask the user to enter a number using input function call, get the length of number using len() function call.

Use the while loop to get the digits of number raised to power of length and get the Armstrong number.

Check if the Armstrong number is equal to number if yes print Armstrong number else not an Armstrong number.


number = int(input("Enter a number : "))
length = len(str(number))
temp = number
sum_of_powers = 0
while( temp != 0 ):
    x = temp % 10
    sum_of_powers = sum_of_powers + x ** length
    temp =  temp // 10
if sum_of_powers == number:
    print("Number is Armstrong")
else:
    print("Number is not an Armstrong")




OUTPUT:

Enter a number : 1741725
Number is Armstrong

Enter a number : 12345
Number is not an Armstrong



 

Conclusion : Try running the above program, comment down below if any queries / suggestions.

 




Post a Comment

0 Comments