-
Notifications
You must be signed in to change notification settings - Fork 0
/
armstrong.c
47 lines (38 loc) · 1.06 KB
/
armstrong.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
In number theory, a narcissistic number in a given number base is a number that is the sum of its own digits each raised to the power of the number of digits.
Narcissistic numbers are also called armstrong numbers.
For example: 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
*/
#include <stdio.h>
#include <math.h>
int num_length(int num);
int armstrong(int num, int length);
void main(){
int n, l, arm;
printf("Enter your number ");
scanf("%d", &n);
if(n <= 0){
printf("Only natural numbers are allowed");
return;
}
l = num_length(n);
arm = armstrong(n, l);
(arm == n) ? printf("%d is armstrong", n) : printf("%d is not an armstrong. The sum of the number raised to the length of number is %d", n, arm);
}
int num_length(int num){
int count = 0;
while(num > 0){
count++;
num /= 10;
}
return count;
}
int armstrong(int num, int length){
int arm = 0, rem;
while(num>0){
rem = num % 10;
arm = arm + pow(rem, length);
num /= 10;
}
return arm;
}