코딩테스트/[C#] 프로그래머스

[프로그래머스 | C# | Lv.0] 홀짝에 따라 다른 값 반환하기

냠냠쿠 2023. 9. 4. 19:58
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/181935

 

📝 나의 풀이

using System;

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