English

Write a program to input any number and check whether given number is Armstrong or not.

Advertisements
Advertisements

Question

Write a program to input any number and check whether given number is Armstrong or not.

Code Writing
Advertisements

Solution

For a three-digit Armstrong number, add the cubes of its digits and compare the sum with the original number.

n = int(input("Enter any number: "))
original = n
sum_of_cubes = 0

while n > 0:
    digit = n % 10
    sum_of_cubes += digit ** 3
    n //= 10

if sum_of_cubes == original:
    print("Armstrong number")
else:
    print("Not an Armstrong number")
 
shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Review of Phython - EXERCISE [Page 26]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 1 Review of Phython
EXERCISE | Q 11. | Page 26
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×