inblog logo
|
moohyun
    알고리즘문제풀기

    짝수의 합

    화낼거양's avatar
    화낼거양
    Nov 21, 2024
    짝수의 합
    Contents
    문제정답
     
     
    https://school.programmers.co.kr/learn/courses/30/lessons/120831
     
     

    문제

     
    notion image
     
    기본적으로 주어진 코드 :
    class Solution { public int solution(int n) { int answer = 0; return answer; } }

    정답

     
    class Solution { public int solution(int n) { int answer = 0; for (int i = n; i > 0; i--) { if (i % 2 == 0) { answer += i; } } return answer; } }
     
    • 반복문을 구성하되, 짝수만 더할 수 있도록 i % 2 == 0 조건일 때만 더하도록 작성하였습니다.
    Share article
    Contents
    문제정답

    moohyun

    RSS·Powered by Inblog