打印所有水仙花数。所谓水仙花是指一个三位数,其各位数字的立方和等于该数,使用c语言
时间: 2024-04-21 11:22:21 浏览: 46
水仙花数是指一个三位数,其各位数字的立方和等于该数本身。你可以使用循环语句来遍历所有的三位数,并判断是否满足水仙花数的条件。以下是一个示例代码:
```c
#include <stdio.h>
int main() {
int num, digit, sum;
printf("所有水仙花数:\n");
for (num = 100; num <= 999; num++) {
sum = 0;
int temp = num;
while (temp > 0) {
digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
if (sum == num) {
printf("%d\n", num);
}
}
return 0;
}
```
在这个代码中,我们使用一个循环来遍历从100到999的所有三位数。对于每个数字,我们将其拆分为个位、十位和百位,分别计算每位数字的立方,并累加到`sum`中。
如果在循环结束后,`sum`的值等于当前的数字`num`,则说明该数字是一个水仙花数,我们将其打印出来。
运行以上代码,你将得到所有的水仙花数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)