编写一个c语言程序,输出所有的水仙花数之和。
时间: 2023-03-15 10:13:44 浏览: 89
答案:#include <stdio.h>
int main()
{
int n, sum = 0, rem, temp;
for(n=100; n<=999; n++)
{
temp = n;
while (temp != 0)
{
rem = temp%10;
sum = sum + rem*rem*rem;
temp = temp/10;
}
if (n == sum)
printf("%d ",n);
sum = 0;
}
return 0;
}
相关问题
编写一个c语言程序输出所有水仙花数
好的,以下是C语言程序输出所有水仙花数的代码:
```
#include <stdio.h>
int main() {
int i, j, k, n;
printf("水仙花数有:\n");
for (n = 100; n <= 999; n++) {
i = n / 100;
j = n / 10 % 10;
k = n % 10;
if (n == i * i * i + j * j * j + k * k * k) {
printf("%d\n", n);
}
}
return 0;
}
```
运行之后,程序会输出所有水仙花数。如果您有其他的问题,欢迎随时问我。
c语言中编写一个程序 输出所有水仙花数
好的,以下是C语言实现输出所有水仙花数的代码:
```c
#include <stdio.h>
int main() {
int num, digit1, digit2, digit3;
printf("所有的水仙花数为:\n");
for(num = 100; num <= 999; num++) {
digit1 = num / 100; // 获取百位数字
digit2 = (num / 10) % 10; // 获取十位数字
digit3 = num % 10; // 获取个位数字
if(num == digit1 * digit1 * digit1 +
digit2 * digit2 * digit2 +
digit3 * digit3 * digit3) {
printf("%d\n", num); // 输出水仙花数
}
}
return 0;
}
```
运行程序后,即可输出所有的水仙花数。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)