What is Armstrong Number?
A three-digit number can be said to be an Armstrong number when the sum of all its individual digits cubes is equal to the number itself.
A positive integer ‘xyz’ is Armstrong number if
xyz... = x^n + y^n + z^n ...
n = number of digits in the integer
3 digit Armstrong number
3^3 + 7^3 + 0^3 = 370
4 digit Armstrong number
1634 = 1^4 + 6^4 + 3^4 + 4^4
Some other example of Armstrong numbers are0, 1, 2, 3, 153, 370, 407, 1634, 8208, etc.
In this tutorial, you will learn to write a python program to check whether a number is Armstrong number or not for 3 digits number aswell as N digit number.
Check 3 Digit Armstrong Number
#Python program to check 3 Digits Armstrong Number #Taking input from user num = int(input("Enter a number: ")) # Declarign and intilizing sum variable arsum = 0 #Coping orignal value into a temp veriable t = num #While loop for iteration till t is greater than 0 while t > 0: #if Yes #Applying Modulation on number d = t % 10 #Applying Formula sum = digit^3 arsum += d ** 3 t //= 10 #checking, Is original number equal to the result if num == arsum: #if Yes, Than print It is an ARMSTRONG NUMBER print(num,"IS AN ARMSTRONG NUMBER") else: #if NO, Than print It is not an ARMSTRONG NUMBER print(num,"IS NOT AN ARMSTRONG NUMBER")
Enter a number: 370 370 IS AN ARMSTRONG NUMBER
in the above example a 3 digit number is obtained from the user using the input method.
This value is converted into an integer using the int() method and assigned to the num variable. Then the arsum variable is assigned the value of 0.
The value of the num variable is then assigned to a temporary variable called t. In the next line, a while loop is executed to check whether the value t is greater than 0. As long as the condition is True, t is divided by 10 using the % operator and the remainder is stored in a variable called d.
Then in the line, arsum += d ** 3; the value in the arsum variable is added to the value of d raised to the power 3. A floor division is carried out in the subsequent line using t//=10.
Then an if statement is used for checking whether the original number is now equal to the value in the arsum variable. If this is True, the statement print(num,”IS AN ARMSTRONG NUMBER“) prints the number along with the string that it is an Armstrong number.
Otherwise, it prints that it is not an Armstrong number.
As per the output, the number entered by the user is 370. As it is an Armstrong number, the string 370 IS AN ARMSTRONG NUMBER is printed.
Check N digit Armstrong Number
#Python program to check n Digits Armstrong Number #Taking input from user number = int(input("Enter a number: ")) # Declarign and intilizing sum variable result = 0 # Declarign and intilizing number of digits variable n = 0 #coping number in another variable originalNumber = number #In this while loop checking the number of digits in entered number #If number is greater than 0 while(originalNumber > 0) : #If Condition is true originalNumber = originalNumber//10 #increasing the digit by 1 on every iteration n = n + 1 #Again coping entered number in another variable because we changed it #during the process of getting the number of digits in number originalNumber = number #In this while loop we are applying the Formula #geting the result on the basis of x^y (x=digit,y=total number of digit) while(originalNumber > 0): #get the last digit of number reminder = originalNumber % 10 #Applying multiplication of digit result = result + reminder ** n #removing the last digit from the number originalNumber = originalNumber // 10 #checking the output of program is equal to the entered number or not if(result == number): #if Yes, Than it is a ARMSTRONG NUMBER print(number,"IS AN ARMSTRONG NUMBER") else: #if No, Than it is not a ARMSTRONG NUMBER print(number,"IS NOT AN ARMSTRONG NUMBER")
Enter a number: 1634 1634 IS AN ARMSTRONG NUMBER
Here, a number is retrieved from the user using the input method and then converted into an integer using the int() method. It is stored in the num variable. The result and n variables are initialized with the value of 0. The value of the number variable is assigned to another variable called originalNumber.
A while loop is executed to check the number of digits in the entered number. While the value of the originalNumber is greater than 0, the // operator is used to divide the value in originalNumber. The variable n is increased by 1 on every iteration. Then the value of the number variable is again copied into the originalNumber variable.
Then a while loop checks if the orginalNumber value is greater than 0. As long as the condition is True, some lines of code execute. First, the value in the originalNumber variable is divided by 10 using the % operator and stored in the remainder variable.
Then in the line, result = result + remainder ** n, the result variable value is added to the remainder raised to the power of n, and the entire value is stored in the result variable. Then the originalNumber variable value is divided by 10 using the // operator to remove the last digit from the number.
At last, an if statement is used for checking whether the result and the number variable have the same value. If the condition is True, the line print(number,”IS AN ARMSTRONG NUMBER”), prints the number along with the string “IS AN ARMSTRONG NUMBER”.
If the condition is False, the number along with the string “IS AN ARMSTRONG NUMBER” is printed to the screen.
According to the output, the number entered by the user is 1634. As it is an Armstrong number,
1634 IS AN ARMSTRONG NUMBER is printed.
The two different ways of determining whether the entered number is an Armstrong number is not are discussed above.
Make sure that you convert the number into an integer using the int() method, when obtaining the input from the user.