In this example, you will learn to check whether an n-digit integer is an Armstrong number or not.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python if...else Statement
- Python while Loop
A positive integer is called an Armstrong number of order n if
abcd... = an + bn + cn + dn + ...
In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example:
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
Source Code: Check Armstrong number [for 3 digits]
# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int[input["Enter a number: "]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
Output 1
Enter a number: 663 663 is not an Armstrong number
Output 2
Enter a number: 407 407 is an Armstrong number
Here, we ask the user for a number and check if it is an Armstrong number.
We need to calculate the sum of the cube of each digit. So, we initialize the sum to 0 and obtain each digit number by using the modulus operator %. The remainder of a number when it is divided by 10 is the last digit of that number. We take the cubes using exponent operator.
Finally, we compare the sum with the original number and conclude that it is Armstrong number if they are equal.
Source Code: Check Armstrong number of n digits
num = 1634
# Changed num variable to string,
# and calculated the length [number of digits]
order = len[str[num]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
You can change the value of num in the source code and run again to test it.
Learn about Armstrong Number in Python.
A number of n-digits can be defined as an Armstrong number if and only if the sum of its digits powered to n is equal to the number itself. For example, 1634 is an Armstrong number as 1634 = 1^4 +6^4 +3^4 + 4^4 The Armstrong number in python is the number in which the sum of each digit powered to the total number of digits is the same as the given number. i.e. for a given number say 153, 1^3 + 5^3 + 3^3 is equal to 153. Take another example, say, 1634 = 1^4 +6^4 +3^4 + 4^4 is an
Armstrong number. Any number in any number system exhibits this sum property, which will be called an Armstrong number. Armstrong numbers are also called Narcissistic numbers. Armstrong numbers don’t have real-life applications. It is just another number puzzle.Overview
Scope
What are Armstrong Numbers?
Algorithm for checking if the given three-digit number is an Armstrong Number in Python
To find a 3-digit Armstrong number, we start by extracting each digit in the number, multiplying it thrice [to calculate its cube] and then adding all those cubes of digits.
Now, the sum will be compared with the given number. If the sum of cubes is the same as the actual number, then we call it an Armstrong number; otherwise, it is not an Armstrong number.
Step 1: Read number
Step 2: Declare a variable sum and initialize it to 0
Step 2: for each digit in the number, multiply it thrice and add it to the sum.
Step 3: check the sum if it is equal to the initial number
Step 4: If both are equal, then print it as an Armstrong number. Else,
print it as not an Armstrong number.
Python Program for checking if the given three-digit number is an Armstrong Number
Code:
number = 153 temp = number add_sum = 0 while temp != 0: k = temp % 10 add_sum += k*k*k temp = temp//10 if add_sum == number: print['Given number is a three-digit Armstrong Number'] else: print['Given number is not an Armstrong Number']
Output:
Given number is a three-digit Armstrong Number
Algorithm for checking if the given n-digit number is an Armstrong Number in Python
To find if an n-digit number is an Armstrong number or not, we will first calculate the number of digits that is equal to n. Then we will extract each digit in the number and multiply it n times by itself [to calculate the nth power of it ] and add all those numbers to the sum variable. Now, we will check if the sum is equal to the given number or not. If both are equal, then we will print it as an Armstrong number or print it as not an Armstrong number.
Step 1: Take input
Step 2: Calculate the number of digits and store it as digits
Step 2: Declare a variable sum and initialize to 0
Step 2: for each digit in the number, multiply the same digit for digits times and then add it to the sum.
Step 3: Check if the sum is equal to the input number or
not
Step 4: Print it as an Armstrong number if it is equal. Else print not an Armstrong number.
Python Program for checking if the given n-digit number is an Armstrong Number
Code:
number = 1634 digits = len[str[number]] temp = number add_sum = 0 while temp != 0: # get the last digit in the number k = temp % 10 # find k^digits add_sum += k**digits # floor division # which update number with second last digit as last digit temp = temp//10 if add_sum == number: print['Given number is an Armstrong Number'] else: print['Given number is not a Armstrong Number']
Output:
Given number is an Armstrong Number
Explanation:
14+64+34+44=16341^4+6^4+3^4+4^4 = 1634
As the sum of digits powered to the number of digits is equal to the number itself, the given number is an Armstrong number.
Conclusion
- We learned about what Armstrong numbers are.
- We also saw how to identify if a number is an Armstrong number or not in Python
- We also learned various algorithms and their implementation to check whether the given number is an Armstrong number.