c语言按照第五章第八题的要求编写判断水仙数的函数,从主函数输入正整数n,在主函数中调用判断水仙花数

2025-04-07 16:29:05
推荐回答(1个)
回答1:

/*C语言的"水仙花数"实现代码*/
#include
#include

#include

int cube ( const int n )

{
return n * n * n;

}

bool isNarcissistic ( const int n )

{

int hundreds = n / 100;

int tens = n / 10 - hundreds * 10;

int ones = n % 10;

return cube(hundreds) + cube(tens) + cube(ones) == n;

}

int main(void)

{

int i;

for ( i = 100; i < 1000; ++ i )

{

if ( isNarcissistic(i) )

printf("%d\n", i );

}
return EXIT_SUCCESS;

}