#include <stdio.h> int main() { int num, originalNum, remainder, result = 0, n = 0; printf("Enter an integer: "); scanf("%d", &num); originalNum = num; // store the number of digits of num in n while (originalNum != 0) { originalNum /= 10; ++n; } originalNum = num; // check if the number is Armstrong while (originalNum != 0) { remainder = originalNum % 10; result += pow(remainder, n); originalNum /= 10; } if (result == num) printf("%d is an Armstrong number.\n", num); else printf("%d is not an Armstrong number.\n", num); return 0; }
Inside the main() function:
- Integer variables `num`, `originalNum`, `remainder`, `result`, and `n` are declared to store the input number, its original value, remainders, result after calculation, and number of digits respectively.
- The user is prompted to enter an integer.
- The number of digits of the input number is calculated and stored in `n`.
- Using a while loop, the sum of nth power of individual digits is calculated.
- If the calculated result is equal to the original number, it is an Armstrong number; otherwise, it is not.
- The program prints the result accordingly.