Python输入一个整数判断是不是水仙数
时间: 2023-04-05 09:03:57 浏览: 90
可以回答这个问题。水仙数是指一个三位数,其各位数字立方和等于该数本身。例如,153是一个水仙数,因为1^3 + 5^3 + 3^3 = 153。以下是Python代码实现:
num = int(input("请输入一个三位数:"))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num, "是水仙数")
else:
print(num, "不是水仙数")
相关问题
python判断一个三为整数是否为水仙花数
水仙花数是指一个三位数各个位上的数字的立方和等于该数本身,例如153就是一个水仙花数,因为1³+5³+3³=153。
以下是Python代码实现判断一个三位整数是否为水仙花数:
```python
num = int(input("请输入一个三位整数:"))
# 获取各个位上的数字
a = num // 100
b = num % 100 // 10
c = num % 10
# 判断是否为水仙花数
if num == a**3 + b**3 + c**3:
print(num, "是水仙花数")
else:
print(num, "不是水仙花数")
```
使用input函数获取用户输入的三位整数,然后将该数分解成百位、十位、个位三个数字,判断它们的立方和是否等于原数,最后输出判断结果。
用Python完成以下任务:输入一个整数,判断该整数是不是水仙数
您好,根据您的要求,我可以回答您的问题。以下是Python代码实现判断一个整数是否为水仙数:
num = int(input("请输入一个整数:"))
sum =
n = len(str(num))
temp = num
while temp > :
digit = temp % 10
sum += digit ** n
temp //= 10
if num == sum:
print(num, "是水仙数")
else:
print(num, "不是水仙数")
希望能够帮到您!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)