코딩테스트/프로그래머스 Lv. 0

[프로그래머스] Lv. 0 홀짝 구분하기

촙오 개발자 2025. 1. 27. 16:07
반응형

홀짝 구분하기

 

문제 설명

 

요구사항

  • 입력받은 숫자가 짝수일 경우 is even 붙여 출력
  • 입력받은 숫자가 홀수일 경우 is odd 붙여 출력

테스트

package lv0;

import java.util.Scanner;

public class 홀짝_구분하기 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		System.out.print(n + " is ");
		
		if(n % 2 == 0) { // 2로 나눠 나머지가 0 일 경우 짝수
			System.out.println("even");
		} else {
			System.out.println("odd");
		}
	}
}

 

프로그래머스

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        System.out.print(n + " is ");
		
		if(n % 2 == 0) {
			System.out.println("even");
		} else {
			System.out.println("odd");
		}
    }
}

 

결과

반응형