Email: rakesh@xamnation.com         Phone/Whatsapp: +91 9988708161
interview questions

Armstrong number in Python

Hello

In this article, we will see how to check whether a number is armstrong number or not in Python.

What is armstrong number

Armstrong numbers, also called narcissist numbers, are those number whose sum of cube of digit is equal to that number.

How to check whether a number is armstrong

Suppose we are given any number n. (d3d3d1). Here d1 is digit at one’ place, d2 is digit at 10s place and d3 is digit at 100th place.

n = d3^3 + d2^3 + d1^3

Python program to see whether a number is armstrong or not

 

def checkNarcissist(number):
	total = []
	count = len(str(number))
	power= int(count)
	digits = [int(x) for x in str(number)]
	for i in digits:
		product = i**power
		total.append(product)
		sumtotal=sum(total)
	if(sumtotal==number):
		print(str(number)+ " is a narcissist number")
	else:
		print(str(number)+ " is not a narcissist number")

checkNarcissist(371)

In this article, we have saw how to check armstrong number in Python. If you have any confusion regarding the code, please do comment in discussion thread below or send us email to info@xamnation.com. 

You can use our code in your website. Kindly do acknowledge us by linking our website or this page. 

About Xamnation

If you are interested, you can reach us at info@xamnation.com for more details. 

Leave a Comment

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