python实现1~100 加法求和,偶数和,奇数和及质数和

 

#for实现
sum=0
for i in range(6):
    sum = sum + i
print (sum)
#while实现
sum = 0
i = 1
while i<=100:
    sum = sum + i      
    i+=1
print (sum)
#偶数和
for i in range(1,101):
    if i%2==0:
        sums1+=i
print(sums1)
#奇数和
for i in range(1,101):
    if i%2!=0:
        sums2+=i
print(sums2)
#质数和
i=2
for i in range(2,100):
    j=2
    for j in range(2,i):
        if i %j ==0:
            break
    else:
            sums3+=i
print(sums3)

 

你可能感兴趣的:(python,python)