Algorithm for Armstrong Number in Python

Algorithm for Armstrong Number in Python | In this post, we will discuss checking the Armstrong numbers algorithm in python 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 example:-

(i) Let’s assume a number as 3, the number of digits is 1 so 3 to the power of 1 is 3 there 3 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. Therefore 371 is an Armstrong number.

Algorithm for Armstrong Number in Python

Now, let’s see the algorithm for the Armstrong number in python. 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 are 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 Code for Armstrong Number

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

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: 3
The number is an Armstrong number.

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

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

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

Enter number: 153
The number is an Armstrong number.

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

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

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 *