🔀📚Intermediate

Check Armstrong Number

Verify Armstrong property

An Armstrong number (narcissistic number) is a number equal to the sum of its digits each raised to the power of the number of digits.
Example: 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153

Program Code

armstrong.c
C
1#include <stdio.h>
2#include <math.h>
3
4int main() {
5 int num, original, remainder, digits = 0;
6 double sum = 0;
7
8 printf("Enter a number: ");
9 scanf("%d", &num);
10
11 original = num;
12
13 // Count digits
14 int temp = num;
15 while (temp != 0) {
16 digits++;
17 temp /= 10;
18 }
19
20 // Calculate sum of digits^n
21 temp = num;
22 while (temp != 0) {
23 remainder = temp % 10;
24 sum += pow(remainder, digits);
25 temp /= 10;
26 }
27
28 if ((int)sum == original) {
29 printf("%d is an Armstrong number\n", num);
30 } else {
31 printf("%d is not an Armstrong number\n", num);
32 }
33
34 return 0;
35}
Output

Enter a number: 153

153 is an Armstrong number

Armstrong Numbers (3-digit)

153, 370, 371, 407