循环语句的经典例题———水仙花数
水仙花数:所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数,如:153就是一个水仙花数,153=1*1*1+5*5*5+3*3*3。
eg:输出所有的水仙花数
#include <stdio.h>
int main()
{
int num, sd, td, hd; //定义三位数num,个位数sd,十位数td,百位数hd
for(num=100;num<1000;num++)//循环所有三位数
{
hd =num/100; //获取三位数字num百位上的数字
td =(num%100)/10;//获取三位数字num十位上的数字
sd =num%10;//获取三位数字num个位上的数字
if(num==hd*hd*hd+td*td*td+sd*sd*sd)
{
printf("水仙花数字:%d\n", num);
}
}
return 0;
}