[코딩테스트/프로그래머스/Java]짝수의 합

Enter·2023년 2월 24일
0

코딩테스트

목록 보기
63/68

🔍짝수의 합

<문제>

정수 n이 주어질 때, n이하의 짝수를 모두 더한 값을 return 하도록 solution 함수를 작성해주세요.


<제한사항>

  • 0 < n ≤ 1000



💡테스트 통과한 코드

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







🔗프로그래머스 - 짝수의 합
https://school.programmers.co.kr/learn/courses/30/lessons/120831

profile
Cherish the moment :)

0개의 댓글