编程实现(for和while各写 遍):
- 求1到100之间所有数的和、平均值 。
for循环:
sum = 0
for x in range(1,101):
sum += x
print("1~100的数的和为%s" "平均值为 %.2f"% (sum,sum/100))
while循环:
sum = 0
count = 1
while count <= 100:
sum+=count
count+=1
print("1~100的数的和为%s \t平均值为 %.2f"% (sum,sum/100))
- 计算1-100之间能3整除的数的和
for循环:
count = 0
for x in range(1,101):
if x % 3 == 0:
count+=x
print(count)
while循环:
count = 1
sum = 0
while count <= 100:
count+=1
if count % 3 == 0:
sum+=count
print("1~100之间能3整除的数的和为%s" % (sum))
- 计算1-100之间 能被7整除的数的和
for循环:
sum = 0
for x in range(1,101):
if x % 7 == 0:
sum+=x
print(sum)
while循环:
sum1 = 0
num = 1
while num <= 100 :
num+=1
if num % 7 == 0:
sum1+=x
print(sum1)
网友评论