1.计算从1到1000以内所有奇数的和并输出。
nums =0
sums =0
while num1<1000:
num +=1
if num1 %2 == 1:
sums1 += nums1
#print()#当遇到nums1是奇数时,打印累加的结果
#print() #每次循坏都打印一次
print() #打印最终结果
#用range()遍历
sums = 0
for i in range(1,1001):
#print(i) #0 1 2 3 ...1000
if i%2==1:
#print(i)
sums += i
print(sums)
2.统计1到100之间可以被7整除的数的个数。
i = 0
count =0
while i<101:
if i%7 ==0:
count += 1
i+=1
print(count)
num = 0
for i in range(1,101): #遍历所有的数,不需要再+1 # 1 2 ... 100
if i%7 == 0:
print(i)
num += 1
print(num)
3.计算从1到100以内所有奇数的和。
i = 1
sums = 0
while i<101:
if i%2 ==0:
sums += 1
i+=1
print(sums)
sums = 0
for i in range(1,101):
if i%2==1:
#print(i)
sums += i
print(sums)
4.计算从1到100以内所有能被3或者17整除的数的和并输出。
i = 0
sums = 0
while i<100:
i+=1
if i%3 == 0 or i%17 ==0:
sums += i
print(sums)
sums = 0
for i in range(1,101):
if i%3 == 0 or i%17 ==0:
print(i)
sums += i
print(sums)
5.计算1到100以内能被7或者3整除但不能同时被这两者整除的数的个数。
num = 0
for i in range(1,101):
if (i%7 ==0 or i%3 ==0) and (i%21 !=0):
print(i)
num +=1
print(num)
x =1
count = 0
while x <=100:
#print(x)
if (x%7 ==0 or x%3 == 0) and not(x%7 ==0 and x%3 == 0):
count += 1
x += 1
print(count)
6.计算1到500以内能被7整除但不是偶数的数的个数。
num = 0
for i in range(1,501):
if (i%7 ==0) and (i%2 !=0):
print(i)
num += 1
print(num)
7.计算从1到1000以内所有能同时被3,5和7整除的数的和并输出。
sums = 0
for i in range(1,1001):
if i%105 ==0:
#if i%3 == 0 and i%5 == 0 and i%7 == 0:
sums += i
print(sums)
8.统计100以内个位数是2并且能够被3整除的数的个数。
num = 0
for i in range(1,101):
if i%10 == 2 and i%3 == 0:
#print(i)
num += 1
print(num)
|