forked from priyankashrama/C-language-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharmstrong.c
More file actions
31 lines (26 loc) · 715 Bytes
/
armstrong.c
File metadata and controls
31 lines (26 loc) · 715 Bytes
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
// filename: armstrong.c
// Compile: gcc armstrong.c -o armstrong
// Run: ./armstrong 153
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <number>\n", argv[0]);
return 1;
}
int num = atoi(argv[1]);
int original = num, sum = 0, digits = 0, temp = num;
while (temp > 0) { digits++; temp /= 10; }
temp = num;
while (temp > 0) {
int digit = temp % 10;
sum += pow(digit, digits);
temp /= 10;
}
if (sum == original)
printf("%d is an Armstrong number.\n", original);
else
printf("%d is not an Armstrong number.\n", original);
return 0;
}