[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. ...