How to Find an Armstrong Number Using Python
An Armstrong number (also called a narcissistic number) is an integer that is equal to the sum of its own digits, each raised to the power of the number of digits. For example, 153 is an Armstrong number because 153 = 13 + 53 + 33. Python provides different methods to check whether the given number is an Armstrong number or not. In this article, we will explore these methods with the help of examples.
Table of Content
What is an Armstrong Number?
First, let us ensure that we all have a good idea of an Armstrong number. It is a positive integer where the sum of each digit powered with the total number of digits equals the number itself.
In other words, a number with n digits is called an Armstrong number of order n, where the sum of each digit powered by n is the same as the number. For example,
All numbers exhibiting this sum property are called Armstrong numbers. They are also known as narcissist numbers.
Now, let’s see how we determine whether a given number is a narcissist:
Best-suited Python courses for you
Learn Python with these high-rated online courses
Methods to Find an Armstrong Number Using Python
For 3-digit numbers (using while loop)
The most common examples for Armstrong numbers are usually three-digit numbers, and hence, first, we will write a program specifically for 3-digit integers:
#Enter inputnum = int(input("Enter 3-digit number : ")) sum = 0temp = num#Define a functionwhile temp > 0: digit = temp % 10 sum += digit * digit * digit temp = temp//10 if sum==num: print('It is an Armstrong number')else: print('It is not an Armstrong number')
Output 1:
Output 2:
What have we done here?
- In the above code again, to check if the 3-digit input is an Armstrong number or not, we will first declare a variable sum and initialize it to 0.
- Next, we run the while loop which calculates the sum of the cube of each digit. This sum is obtained by using the modulus operator %. The remainder of the input number when divided by 10 gives us the last digit of that number.
- Then, we check if the sum is equal to the input number:
- if yes, it is an Armstrong number.
- else, it is not.
Also explore: Introduction to Python Data Types with Examples
- In the above code, we declare three functions –
- The power() function takes two arguments – x and y. In the function body, it calculates x raised to the power y
- The order() function calculates the order of the input number.
- The isArmstrong() function checks whether the given input is an Armstrong number or not.
- In the function body, we will run the while loop, which calculates the sum of the digits, where the order value powers each digit.
- If the condition satisfies, meaning the sum matches the input value, it is an Armstrong number so that the function will return True.
For n-digit numbers(using while loop)
Now, we can simply generalize the above program to find Armstrong numbers having n-digits, as shown:
#Enter inputnum = int(input("Enter 3-digit number : ")) sum = 0temp = num#Define a functionwhile temp > 0: digit = temp % 10 sum += digit * digit * digit temp = temp//10 if sum==num: print('It is an Armstrong number')else: print('It is not an Armstrong number')
Output 1:
Output 2:
What have we done here?
- In the above code again, to check if the n-digit input is an Armstrong number or not, we will first declare a variable sum and initialize it to 0.
- Now, before running the loop, we will first calculate the order of the input number, i.e., the number of digits in the number. This value will be stored in the variable order.
- Next, we will run the while loop, which calculates the sum of the digits, where the order value powers each digit.
- Then, we check if the sum is equal to the input number:
- If yes, it is an Armstrong number.
- Else, it is not.
For n-digit numbers(using functions)
We can also determine if a positive integer is an Armstrong number by using functions in Python:
#Define a function to calculate x raised to the power ydef power(x, y): if y == 0: return 1 if y % 2 == 0: return power(x, y // 2) * power(x, y // 2) return x * power(x, y // 2) * power(x, y // 2) #Define a function to calculate order of the numberdef order(x): #Variable to store of the number n = 0 while (x != 0): n = n + 1 x = x // 10 return n #Define a Function to check whether the given number is Armstrong number or notdef isArmstrong(x): n = order(x) temp = x sum = 0 while (temp > 0): rem = temp % 10 sum = sum + power(rem, n) temp = temp // 10 #The while condition satisfies return (sum == x) #Enter inputx = int(input("Enter n-digit number : ")) print(isArmstrong(x))
Output:
What have we done here?
- In the abIn the above code, we declare three functions –
- The power() function takes two arguments – x and y. In the function body, it calculates x raised to the power y
- The order() function calculates the order of the input number.
- The isArmstrong() function checks whether the given input is an Armstrong number or not.
- In the function body, we will run the while loop, which calculates the sum of the digits, where the order value powers each digit.
- If the condition satisfies, meaning the sum matches the input value, it is an Armstrong number so that the function will return True.
For n-digit numbers(using recursion)
In this method, we find the Armstrong number by using recursion, which is a process where the function calls itself.
#Define a functiondef getSum(num): if num == 0: return num else: return pow((num),order) + getSum(num//10) #Enter inputnum = int(input("Enter n-digit number : ")) #Order of the inputorder = len(str(num)) #Call the functionsum = getSum(num) if sum==num: print('It is an Armstrong number')else: print('It is not an Armstrong number')
Output:
What have we done here?
- The above code declares the getSum() function that accepts an integer argument – num.
- Which calculates the sum of the digits, where the order value powers each digit.
- The order value is the number of digits in the input number. This value will be stored in the variable order.
- Then, we call the function recursively, and if the sum matches the input value, it is an Armstrong number.
Endnotes
Armstrong number is just a number puzzle for beginner programmers to help them grasp the concepts with better understanding. They have no real-world applications as such. Hope this article was helpful for you to understand how to find an Armstrong number using Python.
FAQs
What is an Armstrong number?
An Armstrong number is a number that is equal to the sum of the cubes of its own digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
Can you check for Armstrong number for negative or decimal numbers?
Armstrong number is only defined for positive integers, So, you cannot check for negative or decimal numbers.
How to check Armstrong number for a large number?
The logic is the same as with small numbers, but you need to use a larger data type like long or BigInteger to hold the large number and result of the cubing and summing.
Is it necessary to check for the length of the input number?
Yes, it is necessary to check for the length of the input number, because the length of the input number is used to calculate the sum of cubes of the digits of the number.
What is a Simple Python Code to Check for Armstrong Numbers?
A basic Python code to check for Armstrong numbers includes:
- Taking an input number.
- Calculating the length of the number.
- Summing the digits raised to the power of the length.
- Comparing the sum with the original number to determine if it's an Armstrong number
Can You Implement Armstrong Number Check Using a Function in Python?
Yes, you can create a function to check for Armstrong numbers. This function will:
- Calculate the power of each digit according to the number of digits.
- Sum these powers.
- Compare the sum with the original number to confirm if it's an Armstrong number
What are the Armstrong Numbers Between 1 and 100?
The Armstrong numbers between 1 and 100 are all single-digit numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, as each digit raised to the power of 1 equals the number itself.
Are There Alternate Methods to Check Armstrong Numbers in Python?
Yes, apart from using a while loop, you can also use:
- A for loop for a more concise code structure.
- A recursive function for a different coding approach.
- List comprehension for a compact solution.
- Generators for optimizing memory usage with large numbers
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio