编写一个C程序,输出所有的水仙花数
时间: 2024-02-19 07:58:02 浏览: 30
好的,以下是能够输出所有水仙花数的C程序:
```c
#include <stdio.h>
#include <math.h>
int main() {
int i, num, sum, digit;
for(i = 100; i <= 999; i++) {
num = i;
sum = 0;
while(num > 0) {
digit = num % 10;
sum += pow(digit, 3);
num /= 10;
}
if(sum == i) {
printf("%d\n", i);
}
}
return 0;
}
```
程序中使用了for循环,从100到999遍历所有三位数。在每个数中,将其每个位的数字立方后相加,如果结果等于该数本身,则该数为水仙花数,将其输出即可。
相关问题
编写一个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)
![](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)