Is the Last Digit Even?
Programming Basics · JavaScript
TASK
Problem
Given non-negative `n`, print `YES` if its last digit is even, otherwise `NO`.
EXAMPLE
Example
1234
YES
LIMITS
Constraints
All numeric input values fit in JavaScript `Number`.
LEARN
Theory for this problem
+
### Is the Last Digit Even?
Integer division separates higher decimal/time units, while `%` returns the remainder. These two operations let a program isolate digits or split a total number of seconds/minutes into components.
NEED HELP?
Hints
+
['Decide which part comes from division and which part comes from the remainder.', 'Mentally substitute at least one value for each branch and verify that exactly the intended branch runs.']
ANSWER
Solution
+
const fs = require('fs');
const n = Number(fs.readFileSync(0, 'utf8').trim());
const lastDigit = Math.abs(n) % 10;
console.log(lastDigit % 2 === 0 ? 'YES' : 'NO');