study Log/baekjoon

[baekjoon] 8393번: 합

manyMore 2023. 1. 2. 23:08
java / 단계별로 풀어보기 / 3단계 : 반복문

소스코드
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
        // 상수 n 입력 받기
		int n = sc.nextInt();
        // 더해준 최종 값 초기화
		int sum = 0;
		
        // i가 1부터 시작해 n까지 닿을 동안 + 시켜줌
		for (int i = 1; i <= n; i++) {
            // 합은 i로 시작하여 n에 닿을 때까지 반복하여 더해준 값
			sum = sum + i;
		}
		
		sc.close();
		
		System.out.println(sum);
	}

}
결과