Armstrong Number in Python using While Loop

Armstrong Number in Python using While Loop | In this post, we will discuss the python program to check Armstrong numbers using a while loop with the explanation. Armstrong number is a number that when raised to the power of a number of its own digits is equal to the sum of that number.

For an example of Armstrong number:-

(i) Let’s assume a number as 8, the number of digits is 1 so 8 to the power of 1 is 8 there 8 is an Armstrong number. 

(ii) 371 – there are 3 digits so, each digit will be raised to 3
3*3*3 + 7*7*7 + 1*1*1 = 371. = 371. Therefore 371 is an Armstrong number.

Algorithm for Armstrong Number in Python using While Loop

Now, let’s see the algorithm for the Armstrong number in python using a while loop. An algorithm is a pseudo code to solve the problem, that is it is a step-by-step procedure.

Step 1: Take a number.
Step 2: declare a variable to store the sum and initialize it to 0.
Step 3: find the count of digits in the given number.
Step 4: for each digit in a number multiply it to the count of digits and add it to the sum variable.
Step 5: Check whether the given number and sum is equal or not.
Step 6: if both are equal then print “The number is an Armstrong number” Else print “The number is not an Armstrong number”.

Python Program to Check Armstrong number using While Loop

Let’s see a python program to check Armstrong number using a while loop, while loop is a loop that executes until the condition is false, once the condition becomes false flow comes out of the loop. To find the Armstrong number we need to repeat the same steps several times until the number becomes 0 so we use a while loop.

# Python program to check armstrong number using while loop

# take inputs
number = int(input("Enter number: "))

# find armstrong number
number_new = number2 = number
sum = 0
count = 0
while(number > 0):
   count = count + 1
   number = number//10

while number2 > 0:
   remainder = number2 % 10
   sum += remainder ** count
   number2 //= 10

# check armstrong number or not
if(number_new == sum):
   print("The number is an Armstrong number.")
else:
   print("The number is not an Armstrong number.")

Output for the input values test-case-1:-

Enter number: 8
The number is an Armstrong number.

Output for the input values test-case-2:-

Enter number: 50
The number is not an Armstrong number.

Armstrong Number in Python using While Loop

We also call it the plus perfect number, a pluperfect digital invariant. In the code, we have defined two functions digits_count() and sum() which takes a single parameter number, the first function that is digits_count(n) is defined to count the number of digits in a given number. In the function, we use the while loop to check the number of digits. Next, in the second function sum() we take n as a parameter and call digits_count() to check the number of digits in the number, then in the while loop if the number is greater than 0 we,

Step1: We find mod 10 of the number and store it in digits.
Step2: Then the number is divided by 10 and raise the individual number to the power of the number of its digits, add and store it in s.
Step3: We return the s.
Step4: This process is iterated until the number is 0.

# Python program to check armstrong number using while loop

def digits_count(n):
   i = 0
   while n > 0:
      n //= 10
      i += 1
   return i

def sum(n):
   i = digits_count(n)
   s = 0
   while n > 0:
      digit = n%10
      n //= 10
      s += pow(digit,i)
   return s

# take inputs
num = int(input("Enter number: "))

# calling function
s = sum(num)

# check armstrong number or not
if s == num:
   print("The number is an Armstrong number.")
else:
   print("The number is not an Armstrong number.")

Output for the input values test-case-1:-

Enter number: 370
The number is an Armstrong number.

Output for the input values test-case-2:-

Enter number: 450
The number is not an Armstrong number.

3 Digit Armstrong Number in Python using While Loop

Armstrong number in python for 3 digits is the condition where we find only 3 digit Armstrong numbers.

# Python program to check 3 digit armstrong number using while loop

# take inputs
number = int(input("Enter number: "))

# find armstrong number
temp = number
sum = 0
while temp != 0:
   digits = temp % 10
   sum += digits * digits * digits
   temp = temp // 10

# check armstrong number or not
if sum == number:
   print(number, "is an Armstrong number.")
else:
   print(number, "is not an Armstrong number.")

Output for the input values test-case-1:-

Enter number: 9
9 is not an Armstrong number.

Output for the input values test-case-2:-

Enter number: 153
153 is an Armstrong number.

Any Armstrong number other than 3 digits Armstrong number will be printed as “not an Armstrong number” that is, the above code evaluates only 3 digits of Armstrong number using a while loop.

4 Digit Armstrong Number in Python using while loop

Armstrong number python for 4 digits is the condition where we find only 4 digits of the Armstrong number.

# Python program to check 4 digit armstrong number using while loop

# take inputs
number = int(input("Enter number: "))

# find armstrong number
temp = number
sum = 0
while temp != 0:
   digits = temp % 10
   sum += digits * digits * digits * digits
   temp = temp // 10

# check armstrong number or not
if sum == number:
   print(number, "is an Armstrong number.")
else:
   print(number, "is not an Armstrong number.")

Output for the input values test-case-1:-

Enter number: 153
153 is not an Armstrong number.

Output for the input values test-case-2:-

Enter number: 1634
1634 is an Armstrong number.

Any Armstrong number other than 4 digits Armstrong number will be printed as “not an Armstrong number” that is the above code evaluates only 4 digits Armstrong number using a while loop.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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