![]() |
VOOZH | about |
An Armstrong number is defined as a number that is equal to the sum of the Kth power of each digit in the number, where K is the number of digits in it.
Example:
Input: 153
Output: Yes
Explanation: 153 is an Armstrong number of 3 digits, since the sum of cubes of each digit is equal to the number itself.
13 + 53 + 33= 153Input: 12
Output: No
Explanation: 12 is not an Armstrong number as the sum of square of each digit is not equal to the number:
12+ 22 = 5 ≠ 12
To determine whether the given number is an Armstrong number we need to extract each digit of the number and raise them to the power of the total number of digits and find their sum. If the obtained sum is equal to the original number, then the given number is an Armstrong number, otherwise, it’s not.
Yes
The above method is the most straightforward method to check for Armstrong number in C, but we can also use other methods. Following are some commonly used methods to check for Armstrong number in C:
We can convert the iterative method discussed above into recursive method by considering only a single digit of the number in each recursive call and calling the function again for the number with remaining digits.
Yes
In this method, we convert the number to the numeric string so each digit can be easily accessed. Then easily find the sum of digits raised to the power of number of digits by converting them back to integer and using arithmetic.
Yes