Problem:

Given a natural number n, write code to output "n is even" if n is even, and "n is odd" if n is odd.

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

Constraints:

  • 1 ≤ n ≤ 1,000

Solution:

let a = Int(readLine()!)!
let resultString = a % 2 == 0 ? "\(a) is even" : "\(a) is odd"
print(resultString)

Ternary operator(삼항 연산자) 사용해서 문제를 해결 하였습니다. 입련된 수를 2로 나눈 나머지가 0인지 확인하여 짝수인지 확인할 수 있습니다.

let a = Int(readLine()!)!
print(a, "is", a.isMultiple(of: 2) ? "even" : "odd")

Alternatively, you can also determine even/odd status using Swift's isMultiple(of:) function as shown above.

](