Hello, readers! This article focuses on searching for an Armstrong number using python with the algorithm and examples.
So, let us begin!:)
What is an Armstrong number?
Numbers have always played a crucial and key role in various diversified areas and domains.
While we have various concepts implemented, one of the most heard concepts is the Armstrong number.
A number comprising x digits is considered an Armstrong number only if the summation of every digit raised to the power of the total number of digits (x) is equivalent to the original number.
number - pqr
p^n + q^n + r^n + (x-1)^n
At first, we calculate the number of digits within the total input value. Further, we calculate the power value for every digit with respect to the total digits. Then, we go ahead and perform a summation of all the power values of the digits.
At last, we compare the original value and the summation value. If found the same, it can be termed as an Armstrong number.
Let us now try to implement the above steps to check for the Armstrong number.
Input number = 370
Total number of digits (n) = 3
Evaluation = 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370
As the original number is equal to the final value, it can be termed as an Armstrong number.
Input number = 121
Total number of digits (n) = 3
Evaluation = 1^3 + 2^3 + 1^3 = 1 + 8 + 1 = 10
As the original number is not equal to the final value, it cannot be termed as an Armstrong number.
Algorithm to Find the Armstrong Number
- Accept the input number from the user.
- Then, count the number of digits in the input value (x).
- To count the digit and access it individually, we make use of division and modular operations.
- Now, we raise every digit to the power of the total count of digits (x) and then we store its value into a different variable.
- We need to repeat step 3 and 4 until all the digits get exhausted.
- At last, we compare the results with the original number.
If the results are the same, we declare it as an Armstrong number.
Implementation of Armstrong number search in Python
Have a look at the below code!
num = input() l_digit = len(num) num =int(num) num_n=num ans = 0 while(num!=0): dig = num%10 ans=ans+pow(dig,l_digit) num=int(num/10) if(ans==num_n): print("The number is an Armstrong Number!") else: print("It is not an Armstrong Number!")
- At first, we provision input() function to accept the number from the user.
- The len() function is used to get the total number of digits in the number.
- We then traverse through the number in a digit by digit fashion using a while loop. The loop runs until all the digits are exhausted. Within the loop, we extract the digit and then perform pow() function to get the exponent value for every single digit raised to the power of total digits. Post which, we keep adding the results into the variable ans for every digit.
- Finally, we compare the original number and the result of summation of the pow() function of each digit. If found same, we conclude the number to be an Armstrong number.
153 The number is an Armstrong Number! 111 It is not an Armstrong Number!
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.
For more such posts related to Python programming, Stay tuned with us.
Till then, Happy Learning! 🙂