所謂水仙花數,就是指各位數字的立方和等於該數本身的數字。
下面這個程序將從100到999之間揪出全部的水仙花數。
#include "stdio.h"
void main(void)
{
int i=100;
int a,b,c;
while(i<1000)
{
a=i/100; //百位
b=(i/10)%10; //十位
c=i%10; //個位
if(a*a*a+b*b*b+c*c*c==i)
printf_s("%d 是水仙花數\n",i);
i++;
}
getchar();
}
計算結果
153 是水仙花數
370 是水仙花數
371 是水仙花數
407 是水仙花數