1、求500以内能被3整除或能被5整除的数的和。(分别用while、do while、for编写程序)
时间: 2023-10-28 07:06:07 浏览: 55
JS实现1000以内被3或5整除的数字之和
while:
```python
sum = 0
i = 0
while i <= 500:
if i % 3 == 0 or i % 5 == 0:
sum += i
i += 1
print("和为:", sum)
```
do while:
```python
sum = 0
i = 0
while True:
if i > 500:
break
if i % 3 == 0 or i % 5 == 0:
sum += i
i += 1
print("和为:", sum)
```
for:
```python
sum = 0
for i in range(501):
if i % 3 == 0 or i % 5 == 0:
sum += i
print("和为:", sum)
```
以上三种方法均可求解,且效果相同。
阅读全文