Algorithm/백준알고리즘

[백준알고리즘] 반복문 3단계 - 8393번 합(JAVA)

개발자하소서 2022. 6. 14. 08:27
728x90
반응형
SMALL

📌문제

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

 
 
📌입력

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

 

📌출력

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

📌코드

 

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        for(int i=1; i<=n; i++){
           sum += i;
        }
        System.out.println(sum);
    }
}
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int sum = 0;
        for(int i = 1; i <= n; i++){
            sum += i;
        }
        System.out.println(sum);
    }
}
728x90
반응형
LIST