본문 바로가기
알고리즘/백준

[백준 8393번 ː 자바(JAVA)] 합

by 그릿er 2020. 4. 15.

 

<문제>

 

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

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int num = 0;
        for(int i=1; i<=n; i++) {
            num+=i;
            }
        System.out.println(num);
    }
}