计算100-1000内的水仙花
# include <stdio.h>
//水仙花数
bool shuixianhua(int num);
int main(){
int i;
for (i=100;i<1000;i++){
if(shuixianhua(i) == 1){
printf("%d is a shuixianhua num.\n",i);
}
}
return 0;
}
bool shuixianhua(int num){
int baiwei;
int shiwei;
int gewei;
baiwei = num / 100;
shiwei = num % 100 / 10;
gewei = num % 10;
if (baiwei*baiwei*baiwei + shiwei*shiwei*shiwei + gewei*gewei*gewei == num){
return 1;
}
else{
return 0;
}
}
结果显示: