[Swift] Checking Even or Odd - Programmers Lv.0 (181944)
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. ...