Accept two numbers and a mathematical operator write a program using switch case To display the result depending on operator . Assume that both are non zero number?
Answers
const readline = require("readline")
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const q1 = () => {
return new Promise(resolve => {
rl.question("enter num: ", num => resolve(parseInt(num)))
})
}
const q2 = () => {
return new Promise(resolve => {
rl.question("enter num: ", num => resolve(parseInt(num)))
})
}
const q3 = () => {
return new Promise(resolve => {
rl.question("enter operator: ", op => resolve(op))
})
}
const main = async () => {
let n1 = await q1()
let n2 = await q2()
let op = await q3()
switch (op) {
case "*":
console.log(n1 * n2)
break
case "/":
console.log(n1 / n2)
break
case "+":
console.log(n1 + n2)
break
case "-":
console.log(n1 - n2)
break
default:
console("unsupported operator")
}
rl.close()
}
main()
Supports * / + - operators.
Answer:
const readline = require("readline")
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const q1 = () => {
return new Promise(resolve => {
rl.question("enter num: ", num => resolve(parseInt(num)))
})
}
const q2 = () => {
return new Promise(resolve => {
rl.question("enter num: ", num => resolve(parseInt(num)))
})
}
const q3 = () => {
return new Promise(resolve => {
rl.question("enter operator: ", op => resolve(op))
})
}
const main = async () => {
let n1 = await q1()
let n2 = await q2()
let op = await q3()
switch (op) {
case "*":
console.log(n1 * n2)
break
case "/":
console.log(n1 / n2)
break
case "+":
console.log(n1 + n2)
break
case "-":
console.log(n1 - n2)
break
default:
console("unsupported operator")
}
rl.close()
}
main()