m2skills

armstrong python

Apr 18th, 2017
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. # program to check if a given number is a armstrong number or not
  2.  
  3. def armstrong(number):
  4.     n = number
  5.     myNum = []
  6.     while n != 0:
  7.         temp = n%10
  8.         n = int(n/10)
  9.         myNum.append(temp)
  10.    
  11.     for i in range(len(myNum)):
  12.         myNum[i] = myNum[i] ** 3
  13.  
  14.     sumation = sum(myNum)
  15.     if sumation == number:
  16.         return True
  17.     else:  
  18.         return False
  19.    
  20.  
  21. def checkArmstrong(upper,lower):
  22.     for i in range(lower, upper+1):
  23.         isArmstrong = armstrong(i)
  24.        
  25.         if isArmstrong:
  26.             print(i)
  27.                
  28.  
  29. # main function
  30. cont = True
  31. while cont:
  32.    
  33.     print("The following choices are availiable : ")
  34.     print("1.Check if a number is an Armstrong number or not")
  35.     print("2.Print all armstrong numbers in a given range")
  36.     choice = int(input("Enter your choice : "))
  37.    
  38.     while choice > 2 or choice < 0:
  39.         print("Wrong input!")
  40.         choice = int(input("Enter your choice : "))
  41.    
  42.     if choice == 1:
  43.         number = int(input("Enter the Number : "))
  44.         isArmstrong = armstrong(number)
  45.         if isArmstrong:
  46.             print("The entered number is an Armstrong Number.")
  47.         else:
  48.             print("The entered number is not an Armstrong Number.")
  49.    
  50.     elif choice == 2:
  51.         upper = int(input("Enter the upper limit : "))
  52.         lower = int(input("Enter the lower limit : "))
  53.         checkArmstrong(upper, lower)
  54.    
  55.     n = int(input("Do you want to continue (1/0): "))
  56.     if n == 0:
  57.         cont = False
  58.     print()
Add Comment
Please, Sign In to add comment