c语言打印出水花数,四叶玫瑰数,五角星数等等的数字

求出0~999999之间的所有“水仙花数”并输出。 “水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身, 如;153=13+53+3^3?,则153是一个“水仙花数”。 1234=1^4 + 2^4 + 3^4 +4^4 在数论中,水仙花数(Narcissistic number)也称为自恋数、自幂数、 阿姆斯壮数或阿姆斯特朗数(Armstrong number),是指一N位数, 其各个数之N次方和等于该数。 例如153、370、371及407就是三位数的水仙花数,其各个数之立方和等于该数:

153 = 1^3 + 5^3 + 3^3。

370 = 3^3 + 7^3 + 0^3。

371 = 3^3 + 7^3 + 1^3。

407 = 4^3 + 0^3 + 7^3。

1234 = 14+24+34+44

代码如下:
`在这里插入代码片``#include
#include
int main()
{
int i;
for (i = 0; i <= 999999; i++)
{
int a = i;
int count = 0;
int sum = 0;
while (a)//不要写a!=0,因为a除尽了之后a=0时括号中为假,自然就跳出循环;即可以简化代码
{
count++;
a=a/10;
}//计算出是几位数
a= i;
int b;
while (a)//理由同上哦
{
b= pow((double)(a % 10),(double)count);
sum += b;
a=a/10;
}
if (sum==i)
{
printf("%d\n", i);//注意这里不能写成a哦,不然结果为0,因为跳出循环后a的结果就是0
}
}
return 0;
}
运行结果如下:
c语言打印出水花数,四叶玫瑰数,五角星数等等的数字_第1张图片

你可能感兴趣的:(IT)