Skip to content

Commit 6546da2

Browse files
committed
cal
1 parent 62c03ee commit 6546da2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Programs/Basic/cal.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
//npm install prompt-sync
3+
4+
// Import prompt-sync
5+
const prompt = require("prompt-sync")();
6+
7+
// Take inputs from user
8+
let num1 = Number(prompt("Enter first number: "));
9+
let operator = prompt("Enter operator (+, -, *, /): ");
10+
let num2 = Number(prompt("Enter second number: "));
11+
12+
let result;
13+
14+
// Perform calculation based on operator
15+
if (operator === "+") {
16+
result = num1 + num2;
17+
} else if (operator === "-") {
18+
result = num1 - num2;
19+
} else if (operator === "*") {
20+
result = num1 * num2;
21+
} else if (operator === "/") {
22+
if (num2 !== 0) {
23+
result = num1 / num2;
24+
} else {
25+
result = "Error: Division by zero!";
26+
}
27+
} else {
28+
result = "Invalid operator!";
29+
}
30+
31+
// Show result
32+
console.log(`Result: ${result}`);

0 commit comments

Comments
 (0)