Python Program to Check Armstrong Number

Python Program to Check Armstrong Number

Armstrong number:
A number is called Armstrong number if it is equal to the sum of the cubes of its own digits.
For example: 153 is an Armstrong number since 153 = 1*1*1 + 5*5*5 + 3*3*3.

The Armstrong number is also known as narcissistic number.

See this example:

  1. num = int(input("Enter a number: "))  
  2. sum = 0  
  3. temp = num  
  4.   
  5. while temp > 0:  
  6.    digit = temp % 10  
  7.    sum += digit ** 3  
  8.    temp //= 10  
  9.   
  10. if num == sum:  
  11.    print(num,"is an Armstrong number")  
  12. else:  
  13.    print(num,"is not an Armstrong number")  



Output:
Python Condition And Loops10

No comments:

Post a Comment

Your feedback is highly appreciated and will help us to improve our content.