(1)编写一个Python程序列出100到999之间所有的水仙花数。 (2)编写一个Python程序,判断用户输人的三位数是否为水仙花数。
时间: 2023-06-13 12:06:42 浏览: 57
(1) 编写一个Python程序列出100到999之间所有的水仙花数。
```python
for num in range(100, 1000):
# 将数字转换成字符串并求出每一位上的数字
digit_1 = int(str(num)[0])
digit_2 = int(str(num)[1])
digit_3 = int(str(num)[2])
# 判断是否为水仙花数
if num == digit_1 ** 3 + digit_2 ** 3 + digit_3 ** 3:
print(num)
```
运行程序后,输出结果为:
```
153
370
371
407
```
(2) 编写一个Python程序,判断用户输入的三位数是否为水仙花数。
```python
num = input("请输入一个三位数:")
digit_1 = int(num[0])
digit_2 = int(num[1])
digit_3 = int(num[2])
if int(num) == digit_1 ** 3 + digit_2 ** 3 + digit_3 ** 3:
print(num, "是水仙花数")
else:
print(num, "不是水仙花数")
```
运行程序后,输入一个数字,程序会判断是否为水仙花数,并输出结果。例如,输入153,输出结果为:
```
153 是水仙花数
```
相关问题
用Python,编写一个程序,求解100~999之间所有的水仙花数
以下是Python代码:
```python
for num in range(100, 1000):
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num)
```
输出结果为:
```
153
370
371
407
```
编写一个程序求解100到999之间的所有水仙花数
好的,以下是求解100到999之间的所有水仙花数的程序:
```python
for i in range(100, 1000):
num = str(i)
sum = 0
for digit in num:
sum += int(digit) ** 3
if sum == i:
print(i)
```
输出结果为:
```
153
370
371
407
```
这些数字就是100到999之间的所有水仙花数。
相关推荐
![](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)