maynul67

list all the Armstrong numbers within a range

Jul 29th, 2021
1,424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. def armstr_checker(n):
  2.     sum=0
  3.     count = len(str(n))  #finding no of digits to be used as power
  4.     num1 = n  #temp variable to store value of n
  5.     while num1 > 0:
  6.         last_digit = num1 % 10
  7.         sum = sum + last_digit**count
  8.         num1 = num1 // 10
  9.     if sum == n:
  10.         return True
  11.     return False
  12.  
  13. try:
  14.     num2 = int(input("enter an integer :"))
  15. except ValueError:
  16.     print("invalid input")
  17.  
  18. num2 = int(input("enter an integer :"))
  19. list =[]
  20. for i in range(num2):
  21.     if armstr_checker(i) == True:
  22.         list.append(i)
  23. print(list)
Advertisement
Add Comment
Please, Sign In to add comment