Dremendo Tag Line

Input a number and check if it is an armstrong number or not using a function in Python

Function - Question 8

In this question, we will see how to input a number and check if it is an Armstrong number or not in Python programming using a function. To know more about function click on the function lesson.

Q8) Write a program in Python to input a number and check if it is an Armstrong number or not using a function. The function should return 1 if the numbers is an Armstrong number else return 0. An Armstrong number is a number whose sum of the cube of its digit is equal to the number itself.

Example 153 = 13+53+33

Program

def armstrong(num):
    s=0
    t=num
    while t>0:
        d=t%10
        s=s+(d*d*d)     # sum of cube of the digits
        t=t//10

    if s==num:
        return 1

    return 0


# main program
n=int(input('Enter a number '))
if armstrong(n)==1:
    print('Armstrong Number')
else:
    print('Not Armstrong Number')

Output

Enter a number 153
Armstrong Number
video-poster