백준 8393번 합 / 자바 풀이
for문을 활용한 풀이
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int ans = 0;
for (int i = 1; i <= T; i++) {
ans += i;
}
System.out.println(ans);
}
}
재귀를 활용한 풀이
import java.util.Scanner;
public class Main {
static int ans = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
PLUS(N);
}
private static void PLUS(int n) {
ans += n;
if (n == 1) {
System.out.println(ans);
return;
}
PLUS(n - 1);
}
}