Answer: An n-digit number is Armstrong if the sum of each digit raised to the power n equals the number (e.g. \( 153 = 1^3 + 5^3 + 3^3 \)).
Logic: first count the digits d, then extract each digit with % 10, raise it to the power d and add; compare the sum with the original number.
Program:
#include <stdio.h>
int main()
{
int n, temp, digits = 0, d, i, power, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
/* Count the digits */
temp = n;
do
{
digits++;
temp = temp / 10;
} while (temp != 0);
/* Add each digit raised to the power 'digits' */
temp = n;
while (temp != 0)
{
d = temp % 10;
power = 1;
for (i = 1; i <= digits; i++)
power = power * d;
sum = sum + power;
temp = temp / 10;
}
if (sum == n)
printf("%d is an Armstrong number\n", n);
else
printf("%d is not an Armstrong number\n", n);
return 0;
}Sample output (actual run):
Enter a number: 153
153 is an Armstrong numberEnter a number: 123
123 is not an Armstrong number