codescracker


python

Python Program to Check Armstrong or Not



« Previous Program Next Program »


Check Armstrong or Not in Python

To check whether given number is an Armstrong number or not an Armstrong number in python, you have to ask from user to enter a number to check for Armstrong number as shown in the program given below.

Python Programming Code to Check Armstrong or Not

Following python program ask from user to enter any number to check for Armstrong number:

# Python Program - Check Armstrong Numbers

print("Enter 'x' for exit.");
num = input("Enter any number to check for armstrong: ");
if num == 'x':
    exit();
else:
    number = int(num);
    tot = 0;
    temp = number;
    while temp > 0:
        dig = temp % 10;
        tot += dig ** 3;
        temp //= 10;
    if number == tot:
        print(number,"is an Armstrong Number.");
    else:
        print(number,"is not an Armstrong Number.");

Here is the sample run of the above Python program shows how to check whether a number is an Armstrong number or not:

check armstrong number python

As you have already learned about armstrong number in previous program, that is, generating armstrong number using python, now let's enter any number and find whether the number is armstrong or not as given in below sample run:

find armstrong or not python

Let's re-run the same program and check for a number who is not an armstrong number say 124:

check armstrong python

Below is the same program on python shell:

python check armstrong number

Program Video with Compile and Run

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test