Armstrong Number
- Shreyas Naphad
- May 13, 2024
- 1 min read
Updated: Jun 5, 2024
In this article, we aim to identify whether a given integer is an Armstrong number or not. So in simple terms, an Armstrong number is a number where the sum of its digits raised to the number of digits it has is equal to a given number.
Let us understand this clearly with an example:
N = 153
153 is an Armstrong number because 1^3 + 5^3 + 3^3 is 153
The number of digits over here is 3 so we have raised every digit to 3 and then added them up. This is how we find whether a number is an Armstrong or not. Numbers like 0,1,407 are a few more examples of Armstrong numbers.
Solution:
Explanation:
· We store the Number in original_number and temp variable for future use and make count_digits variable to count the total number of digits present in the number.
· While Number is not equal to 0 we extract every digit and count how many digits are there in the number and store it in the count_digits vaiable.
· Then we create the power_sum variable to calculate the sum of every digit raise to the total number of digits in the number.
· Finally we check whether this power_sum is equal to the original number and if it is then we can say that the number is an Armstrong Number.
Time Complexity: O(log10N + 1)
Space Complexity: O(1)
Comments