How to Check N-Digit Armstrong Numbers in Python [3 Examples]

In this Python post, I will explain in detail how to check n-digit Armstrong numbers in Python with examples.

Armstrong number in Python is a number that, when raised to the power of a number of its digits, is equal to the sum of that number.

I will also cover more topics related to checking Armstrong numbers in Python.

  • Check the 3-digit Armstrong numbers in Python.
  • 4-digit Armstrong number in Python using a function.
  • Check the n-digit Armstrong numbers in Python using a while loop.

How to check the 3-digit Armstrong number in Python

I will show you how to check the 3-digit Armstrong number using a while loop in Python. A while loop in Python executes a set of statements as long as a condition is True.

This is the Python complete program to check the 3-digit Armstrong number using a while loop on Python.

check_armstrong_num = int(input("Enter 3-digit number : "))
armstrong_sum = 0
temp = check_armstrong_num

while temp > 0:
    digit = temp % 10
    armstrong_sum += digit * digit * digit
    temp = temp//10
 
if armstrong_sum==check_armstrong_num:
    print('It is an Armstrong number')
else:
    print('It is not an Armstrong number')

These lines of Python code appear to be a loop that iterates while the variable temp is greater than 0. In each iteration, it processes one digit of the number stored in temp and calculates the sum of cubes of its digits.

while temp > 0:
    digit = temp % 10
    armstrong_sum += digit * digit * digit
    temp = temp//10

Output:

Enter 3-digit number : 401
It is not an Armstrong number

Enter 3-digit number : 407
It is an Armstrong number

You can refer to the screenshot below to see the output of the Python program to check the 3-digit Armstrong numbers.

Armstrong number in Python without using string function

Python program to check 4-digit Armstrong numbers using function

A function in Python is a block of code that only runs when called. A function can return data as a result.

READ:  Encode function in Python string

I will use the function to check the 4-digit Armstrong numbers in Python. This is another method approach to achieve it.

Python’s complete code is to check the 4-digit Armstrong numbers using the function.

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

def armstrong_sum(n):
   i = digits_count(n)
   s = 0
   while n > 0:
      digit = n%10
      n //= 10
      s += digit * digit * digit * digit
   return s

input_number = int(input("Enter number: "))
sum = armstrong_sum(input_number)

if sum == input_number:
   print(input_number, "is a four-digit Armstrong number.")
else:
   print(input_number, "is not a four-digit Armstrong number.")

Output:

Enter number: 1634
1634 is a four-digit Armstrong number.

Refer to the screenshot below to see the output of the Python program to check the 4-digit Armstrong numbers using the function.

Armstrong number in Python using def function

How to check n-digit Armstrong numbers in Python

Let me show you how to check n-digit Armstrong numbers in Python. To achieve this, I will use a while loop.

Below is the complete Python code to check n-digit Armstrong numbers in Python using a while loop.

input_number = int(input("Enter the number : "))
armstrong_sum = 0
armstrong_number = input_number
number_order = len(str(input_number))

while armstrong_number > 0:
    digit = armstrong_number % 10
    armstrong_sum +=  digit ** number_order
    armstrong_number = armstrong_number//10
 
if armstrong_sum==input_number:
    print('It is an Armstrong number')
else:
    print('It is not an Armstrong number')

This while loop iterates while the variable armstrong_number is greater than 0. Each iteration processes one digit of the number stored in armstrong_number and calculates the sum of digits raised to the power of number_order. The ** operator in Python is used for exponentiation.

while armstrong_number > 0:
    digit = armstrong_number % 10
    armstrong_sum +=  digit ** number_order
    armstrong_number = armstrong_number//10

Output:

Enter the number : 548834
It is an Armstrong number

The screenshot below shows the output of the Python program to check the n-digit Armstrong number.

Armstrong number in Python using if else

Conclusion

I hope this article provided the information to check the n-digit Armstrong number in Python. Additionally, I have illustrated the different scenarios to check the Armstrong number in Python, such as 3-digit and 4-digit numbers.

READ:  Merge Dictionaries in Python (8 different methods)

You may also like to read: