1

C语言-水仙花数

计算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;
	}
	
}

结果显示:

posted @ 2023-10-17 16:40  Bonne_chance  阅读(97)  评论(0编辑  收藏  举报
1