밍쎄의 코딩공간

프로그래머스 LV.0 - 홀짝에 따라 다른 값 반환하기 본문

프로그래머스/프로그래머스 LV.0

프로그래머스 LV.0 - 홀짝에 따라 다른 값 반환하기

밍쎄 2023. 7. 29. 23:49

 

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

    }
}

https://school.programmers.co.kr/learn/courses/30/lessons/181935?language=java

728x90