문제 풀이

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

auyeol 2023. 1. 20. 12:13
728x90

문제

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

 

8393번: 합

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

www.acmicpc.net

 

숫자를 입력했을 때, 1부터 입력한 숫자까지의 합을 구하는 문제이다.

 

 

풀이

 

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 = 0; i<=n;i++)
			sum += i;
		
		System.out.println(sum);
	}
}
728x90