개인공부/백준코딩연습
[반복문] - 합
도원결의
2024. 8. 11. 17:11
문제
https://www.acmicpc.net/problem/8393
n이 주어졌을 때, 1부터 n까지 합을 구하는 프로그램을 작성하시오.
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n,total;
n = sc.nextInt();
total = 0;
sc.close();
for(int i = n ; i > 0 ; i-- ) {
total += i ;
}
System.out.println(total);
}
}