프로그래머스/lv0
짝수의 합
do_it0904
2022. 12. 5. 10:14
/* 짝수의 합
* 정수 n이 주어질 때, n이하의 짝수를 모두 더한 값을 return
*
* n result
* 10 30
* 4 6
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class programmer_0_45 { static int a1 = 10; static int a2 = 4; public int solution(int n) { int answer = 0; for(int i = 1; i <= n; i++){ if(i%2 == 0){ answer += i; } } return answer; } public static void main(String args[]){ programmer_0_45 t = new programmer_0_45(); System.out.println("---------------------------------------"); System.out.println("result = " + t.solution(a1)); System.out.println("---------------------------------------"); System.out.println("result2 = " + t.solution(a2)); System.out.println("---------------------------------------"); } } | cs |