본문 바로가기

Python/BOJ

[Python] 합

https://www.acmicpc.net/problem/8393

 

8393번: 합

n이 주어졌을 때, 1부터 n까지 합을 구하는 프로그램을 작성하시오.

www.acmicpc.net

n = int(input())
sum = 0

for i in range(1,n+1):
    sum += i

print(sum)

코드풀이


n = int(input())

sum = 0			# 1~n 까지의 합을 저장할 sum 변수 지정

for i in range(1,n+1):	# range(1,10) 의 경우 1~9 였으므로 n+1을 해줘야 n 값까지 범위가 설정됨
    sum += i

print(sum)

'Python > BOJ' 카테고리의 다른 글

[Python] 백준 10952 A + B - 5  (0) 2021.08.21
[Python] 백준 10871 : X보다 작은 수  (0) 2021.08.21
[Python] A + B - 3  (0) 2021.08.11
[Python] 구구단  (0) 2021.08.11
[Python] 알람 시계  (0) 2021.08.11