3 Digit Armstrong Number in Python

3 Digit Armstrong Number in Python | In this post, we will discuss how to check Armstrong numbers 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.

Example of Armstrong number:-

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

Python Program to Check 3 Digit Armstrong Number

Armstrong number program in python for 3 digits is the condition where we find only 3 digit Armstrong numbers. 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 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 3 digit armstrong number

# take inputs
number = 370

# 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 a three-digit Armstrong number.")
else:
   print(number, "is not a three-digit Armstrong number.")

Output:-

370 is a three-digit 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.

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

# Python program to check 3 digit armstrong number

# 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 a three-digit Armstrong number.")
else:
   print(number, "is not a three-digit Armstrong number.")

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

Enter number: 153
153 is a three-digit Armstrong number.

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

Enter number: 5
5 is not a three-digit Armstrong number.

3 Digit Armstrong Number in Python

We can also take the help of a function to check the 3 digit Armstrong number in python. A function is a block of code that performs a specific task.

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 3 digit 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 += digit * digit * digit 
   return s

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

# calling function
s = sum(num)

# check armstrong number or not
if s == num:
   print(num, "is a three-digit Armstrong number.")
else:
   print(num, "is not a three-digit Armstrong number.")

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

Enter number: 9
9 is not a three-digit Armstrong number.

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

Enter number: 370
370 is a three-digit Armstrong number.

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

Enter number: 1634
1634 is not a three-digit 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 *