-
Notifications
You must be signed in to change notification settings - Fork 1
/
19.c
41 lines (35 loc) · 763 Bytes
/
19.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
// Task : WAP to print Armstrong numbers from 1 to n
#include <stdio.h>
#include <math.h>
// function to find number of digits in a number
int digitsIn(int n){
int digits=0;
if(n==0){
return 1;
}
while(n){
digits++;
n /= 10;
}
return digits;
}
int main() {
int n, i, j, m, sum, digits;
// inputting a number
scanf("%d", &n);
// printing armstrong numbers
printf("all armstrong numbers from 0 to %d are :\n", n);
for(i=0;i<n;i++){
m = i, sum = 0;
digits = digitsIn(i);
while(m){
j = m%10;
sum += pow(j, digits);
m /= 10;
}
if(sum==i){
printf("%d\n", i);
}
}
return 0;
}