본문 바로가기
Programmers 기초

홀짝 구분하기

by lleesla 2024. 3. 13.
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        if(n%2 ==0){
            System.out.println(n+" is even");
        }
        else{
            System.out.println(n+" is 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 "+(n % 2 == 0 ? "even" : "odd"));


    }
}

 

'Programmers 기초' 카테고리의 다른 글

문자열 섞기  (0) 2024.03.13
문자열 겹쳐쓰기  (0) 2024.03.13
문자열 붙여서 출력하기  (0) 2024.03.12
덧셈식 출력하기  (0) 2024.03.12
특수문자 출력하기  (0) 2024.03.09