day 5-5 python (짝수 더하기)
2023. 2. 27. 18:43ㆍ개인적인 공부/Python
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)
아주 간단하고 좋군...
'개인적인 공부 > Python' 카테고리의 다른 글
day 6-1 python 함수 (Functions) (0) | 2023.02.28 |
---|---|
day 5-6 python (Fizz Buzz) (0) | 2023.02.27 |
day 5-4 for 반복문 과 range() (0) | 2023.02.27 |
day 5-2 python (평균 키 구하기) (0) | 2023.02.27 |
day 5-1 for 반복문 (0) | 2023.02.25 |