Safe Divide
JavaScript
Easy
4 views
Problem Description
Two integers a and b are given. If b is 0, handle it safely and print ERR. Otherwise print integer division result (truncate towards zero).
Input Format
One line: a b.
Output Format
One line: result or ERR.
Constraints
a and b are integers, b can be 0.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();if(!s)process.exit(0);const [a,b]=s.split(/\\s+/).map(Number);try{if(b===0)throw new Error('DIV0');process.stdout.write(String(Math.trunc(a/b)));}catch(e){process.stdout.write('ERR');}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!