Python Program to Check Armstrong Number

In this blog, we will discuss what is an Armstrong Number, How to check if the given number is Armstrong or not in Python. A Python program to check Armstrong number will help in building the logicals for solving the problems. The Armstrong number is a number that is equal to the sum of the cubes of its digits. For example 0, 1, 153 and 370 are the Armstrong numbers. Let’s deep dive and see the python program to check armstrong number.

What is the Armstrong Number?

A positive integer will be known as an Armstrong number of order n if

abcd… = an + bn + cn + dn + …
In case of an Armstrong number of 3 digits, the sum of cubes of each digit must be equal to the number itself. For example:

371 = 333 + 777 + 111 = 27 + 343 + 1 // 153 is an Armstrong number.

Algorithm 1 – Check Armstrong Number Using While Loop

  1. Given a number num, count the length of the number and store it in a variable n1 and initialize variables sum with a value 0 and temp with a value num.
  2. Run a while loop with a condition if temp>0 and perform the following operations in it.
    • Initialize cur_dig with value temp%10 i.e. it will be the last digit of the temp number.
    • Add cur_dig**n1 in sum.
    • Update the value of temp to temp//10.
  3. At last check if num == sum, if it is True then print “Number is an Armstrong” else “Number is not Armstrong”.

Code Implementation to check Armstrong Number using while loop

num = int(input("Enter a number: "))

sum = 0
n1 = len(str(num))
temp = num
while temp > 0:
   digit = temp % 10
   sum += digit ** n1
   temp //= 10

if num == sum:
   print(num,"is an Armstrong number")
else:
   print(num,"is not an Armstrong number")
Output
371 is an Armstrong number

Time Complexity

We are looking at each digit of the number, therefore time complexity will be O(X), where X = log10(N) and N is the number which we are checking for being an Armstrong number.

Space Complexity

As We are not using any extra space, therefore the time complexity will be O(1).

Algorithm 2 – Checking Armstrong Number Using Recursion

  1. Take input of integer in num, initialize sum =0, n1 = len(num), and temp equal to num.
  2. Make one function check_armstrong to check if the number is armstrong or not and pass 4 parameters num, n1, sum and temp.
  3. In function perform the following steps:
    • Check the base condition if temp==0. Then,
      • If sum == num return True
      • Else return False
    • Initialize digit with value temp%10
    • Add digit**n1 in sum.
    • Update the value of temp to temp//10.
    • Atlast return the function call with num, n1, sum, temp as parameters.

Code Implementation to check Armstrong Number Using Recursion

def check_armstrong(num,n1,sum,temp):
	if temp==0:
		if sum==num:
			return True
		else:
			return False
	digit = temp % 10
	sum = sum + digit**n1
	temp = temp//10
	return check_armstrong(num, n1, sum, temp)

num = int(input("Enter a number: "))
sum = 0
n1 = len(str(num))
temp = num
res = check_armstrong(num,n1,sum,temp)

if res:
   print(num,"is an Armstrong number")
else:
   print(num,"is not an Armstrong number")
Output
370 is an Armstrong number

Time Complexity

We are looking at each digit of the number, therefore time complexity will be O(X), where X = log10(N) and N is the number which we are checking for being an Armstrong number.

Conclusion

This blog has deeply discussed the python program to check Armstrong number. Coding questions like finding if the given number is an armstrong number are generally asked in the service based companies like TCS, Wipro, Accenture, Capegimini, Infosys and many more. Practice more on logical questions like Finding Armstrong number will increase your logic building skills.

Other Python Programs
Python program to reverse a number
Python program for heap sort
Python program to add two numbers
Python program to check leap year
Python program to convert celsius to fahrenheit
Python program to find factorial of a number
Python program to find the middle of a linked list using only one traversal
Python program to reverse a linked list

Leave a Reply

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