C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python Program to check Armstrong NumberSuppose we have a given number, "K", and we have to check whether the given number is an Armstrong number or not. In this tutorial, we will learn how to identify the given number is an Armstrong number or not using Python. Definition:A positive number of "p" number of digits is called an Armstrong number of order n (order is the number of digits) if: abcde? = pow(a, p) + pow(b, p) + pow(c, p) + pow(d, p) + pow(e, p) + ? Example: Input: 407 Output: Yes, 407 is an Armstrong number Solution: (4 * 4 * 4) + (0 * 0 * 0) + (7 * 7 * 7) = 407 Input: 1634 Output: Yes, 1634 is an Armstrong number Solution: (1 * 1 * 1) + (6 * 6 * 6) + (3 * 3 * 3) + (4* 4 * 4) = 1634 Input: 253 Output: No, 253 is not an Armstrong number Solution: (2 * 2 * 2) + (5 * 5 * 5) + (3 * 3 * 3) = 160 Input: 1 Output: Yes, 1 is an Armstrong number Solution: (1* 1) = 1 Example: def power_1(A, B): if B == 0: return 1 if B % 2 == 0: return power_1(A, B // 2) * power(A, B // 2) return A * power(A, B // 2) * power(A, B // 2) # Function for calculating "order of the number" def order_1(A): # Variable for storing the number N = 0 while (A != 0): N = N + 1 A = A // 10 return N # Function for checking if the given number is Armstrong number or not def is_Armstrong(A): N = order_1(A) temp_1 = A sum_1 = 0 while (temp_1 != 0): R_1 = temp_1 % 10 sum_1 = sum_1 + power_1(R_1, N) temp_1 = temp_1 // 10 # If the above condition is satisfied, it will return the result return (sum_1 == A) # Driver code A = int(input("Please enter the number to be checked: ")) print(is_Armstrong(A)) Output: 1# Please enter the number to be checked: 0 True 2# Please enter the number to be checked: 123 False 3# Please enter the number to be checked: 1634 True ConclusionIn this tutorial, we discussed how to verify whether the given number is an Armstrong number or not. |