Python Program For Armstrong Number

How to Write Python Program For Armstrong Number Using While Loop, For Loop, Functions, and Recursion?. We also show you, Python program to print Armstrong Numbers between 1 to n.

Armstrong Number in Python

If the given number is equal to the sum of the Nth power of each digit present in that integer, then that can be an Armstrong Number in Python. For example, 370 is Armstrong Number.

Individual digits in 370 = 3

370 = 3³ + 7³ + 0³

= 27 + 343 + 0 = 370

The below steps show you the common approach to check for the Armstrong Number in Python

Armstrong Number in Python Steps:

  1. The user has to enter any number.
  2. Count the total individual digits (For Example, 370 means 3).
  3. Divide the given number into individual digits (For Example, Divide 370 into 3, 7, and 0).
  4. Calculate the power of n for each individual and add those numbers.
  5. Compare the original value with Sum value.
  6. If they exactly matched, then it is an Armstrong number else it is not.

Python Program for Armstrong Number Using While Loop

This Python program allows the user to enter any positive integer. And then, this Python program checks whether a number is Armstrong Number or Not using the While Loop

Number = int(input("\nPlease Enter the Number to Check for Armstrong: "))

# Initializing Sum and Number of Digits
Sum = 0
Times = 0
           
# Calculating Number of individual digits
Temp = Number
while Temp > 0:
           Times = Times + 1
           Temp = Temp // 10

# Finding Armstrong Number
Temp = Number
while Temp > 0:
           Reminder = Temp % 10
           Sum = Sum + (Reminder ** Times)
           Temp //= 10
if Number == Sum:
           print("\n %d is Armstrong Number.\n" %Number)
else:
           print("\n %d is Not a Armstrong Number.\n" %Number)

Armstrong Number in Python using a While loop output

Python Program for Armstrong Number Using While Loop

This Armstrong number in python program allows the user to enter any positive integer and then, that assigned it to variable Number.

Next, We assign the original value to the Temp variable. It helps to preserve our original value and then do all the manipulation on the Temp variable.

The first While Loop makes sure that the given number is greater than 0, statements inside the while loop split the numbers and count the total individual digits inside the given integer. If you don’t understand the Python program logic, Please refer Python Program to find Number Of Digits in a Number article.

The Second While loop in this Armstrong Number in python program makes sure that the given number is greater than 0. Let us see the working principle of this while loop in iteration wise

while Temp > 0: Reminder = Temp % 10 Sum = Sum + (Reminder ** Times) Temp //= 10

User Entered value for this Python Program For Armstrong Number : Number= 9474 and Sum = 0

Temp =Number
Temp = 9474

First Iteration

Reminder = Temp %10
Reminder = 9474 % 10 = 4

Sum = Sum + pow (Reminder, Times)

For this Python example, Times =4 because number of digits in 9474 = 4. So,

Sum = Sum + (Reminder * Reminder * Reminder * Reminder)
Sum = 0 + (4 * 4 * 4 * 4) => 0 + 256 => Sum = 256

Temp = Temp /10 => 9474 /10
Temp = 947

NOTE: If the digits count is 5, then Reminder multiplied by 5 times.

Second Iteration: From the Python Program For Armstrong Number first Iteration, the values of both Temp and Sum changed as Temp = 163 and Sum = 256

Reminder = Temp %10
Reminder = 947 % 10 = 7

Sum = 256 + (7 * 7 * 7 * 7) => 256 + 2401
Sum = 2657

Temp = Temp /10 => 163 /10
Temp = 94

Third Iteration: From the second Iteration, the values of Temp = 94 and Sum = 2657.

Reminder = 94 % 10 = 4

Sum = 2657 + (4 * 4 * 4 * 4) => 2657 + 256
Sum = 2913

Temp = 94 /10 = 9

Python Program For Armstrong Number Fourth Iteration

From the third Iteration, the values of Temp = 9 and Sum = 2913

Reminder = 9 % 10 = 0

Sum = 2913 + (9 * 9 * 9 * 9) => 2913 + 6561
Sum = 9474

Temp = 9/10 = 0

Here Temp = 0, so the while loop condition fails.

if ( Number == Sum ) – Condition check whether the user enter is exactly equal to Sum or not. If this condition is True, then it is Armstrong Number else it is not.

Let me check if ( Number==Sum )

if(9474 == 9474) –TRUE, Means Armstrong

NOTE: If you are finding the Python Armstrong number below 1000, remove the while loop to count the digits in a number, and then replace the below code

Sum = Sum + (Reminder ** Times);

With

Sum = Sum + (Reminder * Reminder * Reminder)

Python Program for Armstrong Number Using For Loop

This Python program allows the user to enter any positive integer and then, this program checks whether a number is Armstrong Number or Not using Python For Loop

Num = int(input("\nPlease Enter the Value : "))

Sum = 0
Times = 0
           
Temp = Num
while Temp > 0:
           Times = Times + 1
           Temp = Temp // 10

Temp = Num
for n in range(1, Temp + 1):
           Reminder = Temp % 10
           Sum = Sum + (Reminder ** Times)
           Temp //= 10

if Num == Sum:
           print("\n %d is Armstrong.\n" %Num)
else:
           print("\n %d is Not a Armstrong.\n" %Num)

Please Enter the Value : 371

 371 is Armstrong.



Please Enter the Value : 4458

 4458 is Not a Armstrong.

We just replaced the While loop in the above Armstrong number in Python example with the For loop. If you don’t understand the for loop, then please refer For Loop article here: Python For Loop

Python Program for Armstrong Number Using Functions

This Python program allows the user to enter any positive integer. Then, this program checks whether a number is Armstrong Number or Not using Functions

def Armstrong_Number(Number):
           Sum = 0
           Times = 0

           # Calculating individual digits
           Temp = Number
           while Temp > 0:
                      Times = Times + 1
                      Temp = Temp // 10


           Temp = Number
           for n in range(1, Temp + 1):
                      Reminder = Temp % 10
                      Sum = Sum + (Reminder ** Times)
                      Temp //= 10
           return Sum


#User Input
Number = int(input("\nPlease Enter the Number to Check for Armstrong: "))

if (Number == Armstrong_Number(Number)):
    print("\n %d is Armstrong.\n" %Number)
else:
    print("\n %d is Not an Armstrong.\n" %Number)

Please Enter the Number to Check for Armstrong: 153

 153 is Armstrong.

>>> 

Please Enter the Number to Check for Armstrong: 2256

 2256 is Not an Armstrong.

In this Python Program For Armstrong Number example, we defined the following function to perform all the necessary calculations and return Sum.

def Armstrong_Number(Number):

When the compiler reaches the following code inside the If statement then, the compiler immediately jumps to the above-specified function.

Armstrong_Number(Number)

We already explained the LOGIC above example.

Python Program for Armstrong Number Using Recursion

This program allows us to enter any positive integer. Next, this Python program checks whether a number is Armstrong or Not using Recursion concept.

Sum = 0
Times = 0

def Count_Of_Digits(Number):
           global Times
           if(Number > 0):
                      Times = Times + 1
                      Count_Of_Digits(Number // 10)
           return Times

def Armstrong_Number(Number, Times):
           global Sum
           if(Number > 0):
                      Reminder = Number % 10
                      Sum = Sum + (Reminder ** Times)
                      Armstrong_Number(Number //10, Times)
           return Sum

Number = int(input("\nPlease Enter the Number : "))

Times = Count_Of_Digits(Number)
Sum = Armstrong_Number(Number, Times)
if (Number == Sum):
    print("\n %d is Armstrong.\n" %Number)
else:
    print("\n %d is Not a Armstrong.\n" %Number)

Please Enter the Number : 153

 153 is Armstrong.

>>> 

Please Enter the Number : 259

 259 is Not a Armstrong.

In this Python Program for Armstrong Number example, we defined two recursive functions. The following function accept integer values as parameter value and count the total individual digits in a number recursively.

def Count_Of_Digits(Number):

The following function accepts two integer values as parameter values. And it performs all the necessary calculations and return Sum.

def Armstrong_Number(Number, Times):

The following Statement in this Python Program For Armstrong Number help to call the function Recursively with updated value. If you miss this statement, then after completing the first line it will terminate. For example,

Armstrong_Number(Number //10, Times)

Number= 153

Then the output = 27

Let’s see the If statement inside the above-specified functions

if (Number> 0) check whether the number is greater than 0 or not. For Recursive functions, it is essential to place a condition before using the function recursively. Otherwise, we end up in infinite execution (Same like infinite Loop).

Python Program to Find Armstrong Numbers between the 1 to n

This program allows you to enter minimum and maximum values. This Python program returns Armstrong Numbers between the Minimum and Maximum values.

The first two statements present in this Armstrong number in python program allows the user to enter minimum and maximum values.

The Python for Loop helps to iterate between Minimum and Maximum Variables. Iteration starts at the Minimum, and then it won’t exceed the Maximum variable.

if(Number== Sum) -– condition check whether the sum of the power N for each digit present in that integer is equal to a given number or not. When the condition is True, it is an Armstrong else it is not.

If this condition is True, it prints that number.

Minimum = int(input("Please Enter the Minimum Value: "))
Maximum = int(input("\n Please Enter the Maximum Value: "))

for Number in range(Minimum, Maximum + 1):
           Sum = 0
           Times = 0
           
           Temp = Number
           while Temp > 0:
                      Times = Times + 1
                      Temp = Temp // 10

           Temp = Number
           while Temp > 0:
                      Reminder = Temp % 10
                      Sum = Sum + (Reminder ** Times)
                      Temp //= 10
           if Number == Sum:
                      print(Number)

Armstrong Numbers from 10 to 10000 output

Please Enter the Minimum Value: 10

 Please Enter the Maximum Value: 10000
153
370
371
407
1634
8208
9474