number = input("Enter a number:")
length = len(number)
number = int(number)
temp = number
sum = 0
while number > 0:
rem = number % 10
sum = sum + rem**length
number = number//10
if temp == sum:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
Enter a number: 153
Armstrong Number
Enter a number: 154
Not an Armstrong Number
Armstrong number is a number that equals the sum of the digits, each digit is raised to the power of the number of digits.
Examples of armstrong numbers and non-armstrong numbers:
Example #1
153 is armstrong number:
153
number of digits = 3
1*1*1 + 5*5*5 + 3*3*3 = 153
Example #2
1634 is armstrong number:
1634
number of digits = 4
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634
Example #3
152 is not an armstrong number:
152
number of digits = 3
1*1*1 + 5*5*5 + 2*2*2 = 134
Here i will explain the code with an example.
number = input("Enter a number:")
The input() function is used to get input from the user during runtime.
For example: if the user input is 153, it will be assigned to the variable 'number' as str(string) object.
number = "153"
length = len(number)
The length of the str(string) object is returned by the len() function and is assinged to the variable 'length'.
length = 3
number = int(number)
The number "153" which is stored as string is now get converted into integer using int() function and is assigned to the variable 'number'.
number = 153
temp = number
The intrger value 153 stored in the varible 'number' is also assigned to a temporary variable 'temp'.
temp = 153
sum = 0
The variable 'sum' is initialised to zero.
while number > 0:
rem = number % 10
sum = sum + rem**length
number = number//10
#First loop
While number > 0:
=> while 153 > 0: which is True, therefore, the body of the loop will be executed:
rem = 153 % 10
=> rem = 3
By doing the above operation, the rem variable is assigned with the last digit of the number 153, ie. number 3
sum = sum + rem**length
=> sum = 0 + 3**3
=> sum = 0 + 27
=> sum = 27
By doing the above operation, the variable is stored with the value of remainder to the power of length, ie. 3 to the power of 3. Currently the value of the variable 'sum' is 0. So, 0 + 3 to the power of 3 is assigned to the variable sum, ie. 27.
number = number//10
number = 153 // 10
number = 15
Before going to the next loop, we need to have only the first two digits of number 153 because the operation on last digit is already performed in the current loop(first loop). The floor division of number 153 by 10 is 15 (integer value is only assigned back to variable 'number').
By the end of the first loop, the current values of variables are as shown below.
sum = 27
number = 15
#Second loop
While number > 0:
=> while 15 > 0: which is True, therefore, the body of the loop will be executed:
rem = 15 % 10
=> rem = 5
By doing the above operation, the rem variable is assigned with the second digit of the three digit number 153, ie. number 5
sum = sum + rem**length
=> sum = 27 + 5**3
=> sum = 27 + 125
=> sum = 152
By doing the above operation, the variable is stored with the value of remainder to the power of length, ie. 5 to the power of 3. Currently the value of the variable 'sum' is 27. So, 27 + 5 to the power of 3 is assigned to the variable 'sum', ie. 152.
number = number//10
number = 15 // 10
number = 1
Before going to the next loop, we need to have only the first digit of number 15 because the operation on digit 5 is already performed in the current loop(second loop). The floor division of number 15 by 10 is 1 (integer value is only assigned back to variable 'number').
By the end of the second loop, the current values of variables are as shown below.
sum = 152
number = 1
#Third loop
While number > 0:
=> while 1 > 0: which is True, therefore, the body of the loop will be executed:
rem = 1 % 10
=> rem = 1
By doing the above operation, the rem variable is assigned with the first digit of the three digit number 153, ie. number 1
sum = sum + rem**length
=> sum = 152 + 1**3
=> sum = 152 + 1
=> sum = 153
By doing the above operation, the variable is stored with the value of remainder to the power of length, ie. 1 to the power of 3. Currently the value of the variable 'sum' is 152. So, 152 + 1 to the power of 3 is assigned to the variable sum, ie. 153.
number = number//10
number = 1 // 10
number = 0
Before going to the next loop, we need to have only the remaining digits of number 1 because the operation on digit 1 is already performed in the current loop(third loop). The floor division of number 1 by 10 is 0 (integer value is only assigned back to variable 'number'). But the value of variable 'number' is now zero, which means operations are performed on all the digits of the number 153 and no more operations are left unperformed.
By the end of the third loop, the current values of variables are as shown below.
sum = 153
number = 0
#Fourth loop [not executed]
While number > 0:
=> while 0 > 0: which is False, therefore, the body of the loop will not be executed and control of the loop comes outside the loop.
Now we need to compare the value of the variable 'sum' with the actual number inputted by the user, ie.153. But the variable 'number' is not having the value 153 intially assigned by the user. But we have already have a copy the number 153 that is assigned to variable 'temp'.
if temp == sum:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
So we need to compare the values of variables 'sum' and 'temp'.
If both are same, then it's an armstrong number. Otherwise it's not.
if temp == sum:
=> if 153 == 153: which is True, therefore, the body the if conditional statement will be executed. ie. 'print("Armstrong Number")' will get executed and the output will be 'Armstrong Number'.