Tiny Star

๐Ÿ”“Codingtest/0๏ธโƒฃLv.0

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ง์ˆ˜์˜ ํ•ฉ - Java

hymiel 2023. 6. 13. 21:47

2023๋…„ 06์›” 13์ผ ์š”์ผ

[๋ฌธ์ œ ์„ค๋ช…]

์ •์ˆ˜ n์ด ์ฃผ์–ด์งˆ ๋•Œ, n์ดํ•˜์˜ ์ง์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

[๋‚ด ํ’€์ด]

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int solution(int n) {
    int answer = 0;
    if ( n > 0 && n <= 1000) {
        for(int i = 1; i <= n; i++) {
            if(i % 2 == 0) {
            answer += i;
            }
        }
    }
    return answer;
}