본문 바로가기

프로그래머스/lv0

짝수의 합

/* 짝수의 합
 * 정수 n이 주어질 때, n이하의 짝수를 모두 더한 값을 return
 *
 * n    result
 * 10   30
 * 4    6
 */
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class programmer_0_45 {
    static int a1 = 10;
    static int a2 = 4;
    public int solution(int n) {
        int answer = 0;
        for(int i = 1; i <= n; i++){
            if(i%2 == 0){
                answer += i;
            }
        }
        return answer;
    }
    public static void main(String args[]){
        programmer_0_45 t = new programmer_0_45();
 
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

나이출력  (0) 2022.12.05
각도기  (0) 2022.12.05
양꼬치  (0) 2022.12.05
머쓱이보다 키 큰 사람  (0) 2022.12.05
중복된 숫자의 개수  (0) 2022.12.05