Armstrong Number The Python Way

What is Armstrong number

Armstrong number of three digits is an integer such that the sum of the cubes of the digits is equal to the number itself.

The game works with a timer so we give a player time  to think.

It's modified to ask your name.

Fun with Visual Studio

Let's have fun generating Armstrong Number

We find the sum of the cube of each digits

temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3

The Python logic

  1. # Python program to check if the number provided by the user is an Armstrong number or not  
  2.  
  3. # take input from the user  
  4. num = int(input("Enter a number: "))  
  5.  
  6. # initialise sum  
  7. sum = 0  
  8.  
  9. # find the sum of the cube of each digit  
  10. temp = num  
  11. while temp > 0:  
  12. digit = temp % 10  
  13. sum += digit ** 3  
  14. temp //= 10  
  15.  
  16. # display the result  
  17. if num == sum:  
  18. print(num,"is an Armstrong number")  
  19. else:  
  20. print(num,"is not an Armstrong number"
Open up Visual Studio

After that open a Visual Studio project with Python,

project

The program

code

Will try with 2 numbers.

Let's try 346,

numbers

numbers

Now 371,

It's an Armstrong Number.

Cubing numbers: 3*3*3 + 7*7*7 + 1= 371 (Armstrong Number)

Armstrong

Conclusion

We will go through lots of tricky logic and a simple one with Visual Studio in Python. We see it's very easy to implement it.
 
Read more articles on Python:

 

Up Next
    Ebook Download
    View all
    Learn
    View all