개인적인 공부/Python
day 5-5 python (짝수 더하기)
karatejin
2023. 2. 27. 18:43
Instructions
You are going to write a program that calculates the sum of all the even numbers from 1 to 100. Thus, the first even number would be 2 and the last one is 100:
i.e. 2 + 4 + 6 + 8 +10 ... + 98 + 100
Important, there should only be 1 print statement in your console output. It should just print the final total and not every step of the calculation.
2 부터 100까지 짝수만 더하기
나의 코드
## 짝수 더하기
total = 0
for even in range(2,101):
if even % 2 == 0:
total +=even
print(total)
다른 방법
## 다른 방법
for number in range (2,101,2): ## 2 부터 100 까지 2를 더해서 출력한다 즉 짝수를 출력한다.
total += number
print(total)
아주 간단하고 좋군...