An Armstrong Number is a number that when you raise each digit of the number to the power of the number of digits and then add them up you get the original number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <math.h> #include <stdio.h> int main() { int num, originalNum, remainder, n = 0; float result = 0.0; printf("Enter an integer: "); scanf("%d", &num); originalNum = num; // store the number of digits of num in n for (originalNum = num; originalNum != 0; ++n) { originalNum /= 10; } for (originalNum = num; originalNum != 0; originalNum /= 10) { remainder = originalNum % 10; // store the sum of the power of individual digits in result result += pow(remainder, n); } // if num is equal to result, the number is an Armstrong number if ((int)result == num) printf("%d is an Armstrong number.", num); else printf("%d is not an Armstrong number.", num); return 0; } |
Output:
1 2 3 4 5 | <samp>Enter an integer: 1634 1634 is an Armstrong number. </samp> |
In this program, the number of digits of an integer is calculated first and stored in n. And, the pow() function is used to compute the power of individual digits in each iteration of the second for loop.
