본문 바로가기

Coding Test/프로그래머스

[프로그래머스/자바] 짝수의 합

class Solution {
    public int solution(int n) {
        int answer = 0;
        for(int i=2; i<=n; i+=2){
            answer += i;
        }
        return answer;
    }
}

 


import java.util.stream.IntStream;
class Solution {
    public int solution(int n) {
        return IntStream.rangeClosed(0, n)
            .filter(e -> e % 2 == 0)
            .sum();
    }
}

다른 분의 풀이. 참고하고 싶어서 가져왔다.