algorithm/백준

[백준] 합 - 8393 Java[자바]

달마루 2022. 12. 18. 23:32

[Bronze V] 합 - 8393

문제 링크

 

8393번: 합

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

www.acmicpc.net

성능 요약

메모리: 17620 KB, 시간: 220 ms

분류

구현(implementation), 수학(math)

문제 설명

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

입력

첫째 줄에 n (1 ≤ n ≤ 10,000)이 주어진다.

출력

1부터 n까지 합을 출력한다.

답 : 입력해준 값 만큼 1부터 계속 더해준다. 

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int sum = 0;
        sc.close();
        for (int i = 1; i <= a; i++) {
            sum += i;
        }
        System.out.println(sum);
    }
}