Python Program to Check Armstrong Number

Python Program to Check Armstrong Number is one of the basic program in learning python programming language. The property of Armstrong number is, if the sum of the cubes of the digits of number is same as the given original number, then that number is treated as Armstrong Number. The number 153 is regarded as Armstrong number because 1^3 + 5^3 + 3^3 = 153(1+125+27). Now we shall writing a program to check whether given number is armstrong or not.

1. We need to get the input from the user using input() function.
2. Later on we need to convert the the given input to int data type using inbuilt int() function.
3. The main logic is, we need to identify every digit of the given number. This is done using / and % operator upon the given number by 10.
4. If a number is / by 10, then output is quotient i.e all digits of the given number except Last ONE.
For eg: 152/10 = 15.
5. If a number is % by 10, then output is last digit of the number.
For eg: 152%10 = 2.
6. Hence, by running a while loope until number becomes zero will gives us all the digits of the input number.

num = input("Enter number to check for Armstrong:")
result = 0;
temp = int(num)
while(temp>0):
	remainder= int(temp%10)
	temp=temp/10
	result=result+(remainder*remainder*remainder)
if(int(result) == int(num)):
	print("Given number " + num +" is an Armstrong number.");
else:
	print("Given number " + num +" is not an Armstrong number.");

Output:
armstrong

Download the Source Code

Advertisement

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Python and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s