본문 바로가기
코딩테스트/프로그래머스 기초

[코딩테스트 Lv.0] 홀짝 구분하기

by 블루데이제이 2025. 4. 9.
728x90
반응형
문제 설명

자연수 n이 입력으로 주어졌을 때 만약 n이 짝수이면 "n is even"을, 홀수이면 "n is odd"를 출력하는 코드를 작성해 보세요.

제한사항
1 ≤ n ≤ 1,000
입출력 예
입력 #1
100

출력 #1
100 is even

입력 #2
1

출력 #2
1 is odd

내 풀이
Java
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.println(String.format("%d is %s", n, n % 2 == 0 ? "even" : "odd"));
    }
}
C
#include <stdio.h>

int main(void) {
    int a;
    scanf("%d", &a);    
    printf("%d is %s", a, a % 2 == 0 ? "even" : "odd");    
    return 0;
}
Python
a = int(input())

print(a, "is", "even" if a % 2 == 0 else "odd")

https://school.programmers.co.kr/learn/courses/30/lessons/181944

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

728x90
반응형