Python Program to check whether a given number is Armstrong or not
Program to check whether a given number is Armstrong or not
A number is said to be an Armstrong if the sum of each digit's cube of a given number equals the original number.
Given number 153 1*1*1 + 5*5*5 + 3*3*3 = 153 So the given number is Armstrong.
The following is a source code to detect Armstrong number in Python:
#Program to a given number is Armstrong #Take user input number = int(input("Enter the number:")) #Initialize sum sum = 0 #Compute the sum of cube of each digit temp = number while(temp > 0): digit = temp%10 sum += digit**3 temp //= 10 #Check the result if number == sum: print(number, "is an Armstrong number.") else: print(number, "is not an Armstrong number.")
Explanation: In this program, a user is first asked to enter a number, and this number will store in different variables. We have declared a variable sum and initialized its value 0. Next, store the number in a temporary variable, and obtain each digit number using the modulus operator %. When the remainder of a number is divided by 10, we will get the last digit of that number. We take the cubes using the exponent operator. Finally, it will compare the sum with the original number to check the Armstrong number.