
자연수 n이 입력으로 주어졌을 때 만약 n이 짝수이면 "n is even"을, 홀수이면 "n is odd"를 출력하는 코드를 작성해 보세요.
n ≤ 1,000입력 #1
100
출력 #1
100 is even
입력 #2
1
출력 #2
1 is odd
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
input = line.split(" ");
}).on("close", function () {
const n = Number(input[0]);
console.log(n % 2 === 0 ? `${n} is even` : `${n} is odd`); // 조건에 따라 출력
});
line.split(" ")을 통해 입력값을 공백 기준으로 분리하여 input 배열에 저장합니다.input[0])을 Number(input[0])으로 변환하여 n 변수에 저장합니다.? :)를 사용하여 n % 2 === 0 조건을 평가합니다.n은 짝수이므로 ${n} is even을 출력합니다.n은 홀수이므로 ${n} is odd을 출력합니다.100일 경우 100 is even이 출력됩니다.1일 경우 1 is odd가 출력됩니다.입력:
100
출력:
100 is even