본문 바로가기

JAVA/JAVA Programming

[JAVA] 1부터 임의 값까지의 합

반응형
import java.io.*;
public class Plus {
   
    public static void main(String[] ar) throws IOException {
   
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       
        System.out.print("수를 입력하세요 : ");
        int x = Integer.parseInt(in.readLine());
        int tot = 0;
       
        for(int a= 1; a <= x; a++) {
           
            tot += a;    // tot = tot + a;
        }
       
        System.out.print("1에서 " + x + "까지의 합은 " + tot + "입니다.");
    }
}


반응형