Armstrong Number The Python Way

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


Similar Articles