Java/기타 알고리즘

22부터 76까지의 짝수의 개수와 총합을 구하세요.

은찡안찡 2023. 4. 5. 17:42

문제> 22부터 76까지의 짝수의 개수와 총합을 구하세요.

 

package com.dream.controls;

public class OverLapEx04 {
	public static void main(String[] args) {
		//1. 변수 선언
		int count = 0, total = 0;
		
		//2. 더하는 명령어가 반복되고 있다. > 총합 > 반복 횟수가 정해져 있기 때문에 for 문을 사용한다.
		/*
		for(int i=22; i<=76; i++) {
			if(i % 2 == 0) {
				count++;
				total += i; //total = total + i;
			}			
		}
		*/
		for(int i=22; i<=76; i+=2) {
			count++;
			total += i; //total = total + i;						
		}
		
		System.out.println("짝수의 개수: "+count);
		System.out.println("짝수의 총합: "+total);
	}
}