Program to find all Armstrong numbers between 0 and 999
An Armstrong number of three digit is a number such that that sum
of the cubes of it's digits is equal to the number itself
For example 153 is an Armstrong number because cube of 1 is 1(1x1x1=1) + cube of 5 is 125(5*5*5=125) + cube of 3 is 27(3*3*3=27). Now add all the cubes 1+125+27=153 which is equals to number itself
1 |
#include <stdio.h> |
2 |
#include <conio.h> |
3 |
#include <strings.h> |
4 |
#include <math.h> |
5 |
|
6 |
int noOfDigits(int n){ |
7 |
unsigned int number_of_digits = 0; |
8 |
|
9 |
do { |
10 |
++number_of_digits; |
11 |
n /= 10; |
12 |
} while (n); |
13 |
|
14 |
return number_of_digits; |
15 |
} |
16 |
|
17 |
|
18 |
int main() |
19 |
{ |
20 |
int num,rem,qub,sum=0,i,len; |
21 |
printf("Armstrong numbers between 0 to 999 are: "); |
22 |
|
23 |
for(i=0; i<=999; i++) |
24 |
{ |
25 |
num=i; |
26 |
sum=0; |
27 |
len=noOfDigits(i); |
28 |
while(num>0) |
29 |
{ |
30 |
rem=num%10; |
31 |
qub=pow(rem,len); |
32 |
sum=sum+qub; |
33 |
num=num/10; |
34 |
} |
35 |
if(sum==i) |
36 |
{ |
37 |
printf("%d,",sum); |
38 |
} |
39 |
} |
40 |
getch(); |
41 |
} |
42 |
|
43 |
http://www.sachinpuri.com/Article/61/what_is_an_armstrong_number.html |