Python Program to Check Armstrong Number

Introduction

In this post, I am going to write a python program to check armstrong number. We will read a number from the user and will check whether that number is armstrong or not.

What is Armstrong Number?

Suppose the input number is 153. Now, if we add cube of each digits and if we get the same number then we can say that this number is armstrong number. For example, 13 + 53 + 33 = 1 + 125 + 27 = 153. Therefore, we can 153 is armstrong number.

Take another number i.e. 123. Now, the addition of cube of each digits of 123 should be 123, otherwise that number will not be armstrong number. So the 13 + 23 + 33 = 1 + 8 + 27 = 36 which is not 123. Therefore, we can say 123 is not an armstrong number.

Expected Output

Enter a number: 153
153 is an Armstrong Number

Enter a number: 123
123 is not an Armstrong Number

Python Program to Check Armstrong Number

# Python Program to Check Armstrong Number
def armstrong_number(num):
    rem = 0
    sum = 0
    while num > 0:
        rem = num % 10
        sum = sum + rem * rem * rem
        num = num // 10
    return sum


number = int(input("Enter a number: "))
sum_cubes_number = armstrong_number(number)
if number == sum_cubes_number:
    print(f"{number} is an Armstrong Number")
else:
    print(f"{number} is not an Armstrong Number")

I hope you have understood this program. If you need explanation for this program, please contact me or comment here, I will add explanation also.

Thank you.

Leave a Comment