작성일 :

문제 링크

10480번 - Oddities

설명

주어진 정수가 홀수인지 짝수인지 판별하는 문제입니다.

각 정수에 대해 x is even 또는 x is odd 형식으로 출력합니다.


접근법

정수를 2로 나눈 나머지가 0이면 짝수, 아니면 홀수입니다.

각 테스트 케이스마다 판별 결과를 형식에 맞게 출력합니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
using System;

class Program {
  static void Main() {
    var t = int.Parse(Console.ReadLine()!);
    for (var i = 0; i < t; i++) {
      var x = int.Parse(Console.ReadLine()!);
      var res = (x % 2 == 0) ? "even" : "odd";
      Console.WriteLine($"{x} is {res}");
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t; cin >> t;
  while (t--) {
    int x; cin >> x;
    cout << x << " is " << ((x % 2 == 0) ? "even" : "odd") << "\n";
  }

  return 0;
}