0% found this document useful (0 votes)
11 views3 pages

Ex 9 Simple Calculator Application

The document outlines the procedure to develop a simple calculator application using Node.js for basic arithmetic operations. It includes steps for setting up the development environment, creating application files, and running the application. The result demonstrates successful execution of the calculator with various operations.

Uploaded by

hemnathsingh26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Ex 9 Simple Calculator Application

The document outlines the procedure to develop a simple calculator application using Node.js for basic arithmetic operations. It includes steps for setting up the development environment, creating application files, and running the application. The result demonstrates successful execution of the calculator with various operations.

Uploaded by

hemnathsingh26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Ex: No: 9 SIMPLE CALCULATOR APPLICATION (Beyond Syllabi)

DATE :

AIM:
To develop a simple calculator application using node.js for basic arithmetic operations.

PROCEDURE:

Set up your development environment:

➢ Install Visual Studio Code (if you haven't already).


➢ Install Node.js and npm (Node Package Manager) to manage dependencies.

Step 1: Set Up the Project & Initialize it


Create a new directory for your project, initialize npm, and install dependencies.
mkdir calculator-app
cd calculator-app
npm init -y
Step 2: Create the Application Files
Create a file called calculator.js
const readline = require('readline');
// Set up the interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Functions for basic arithmetic operations
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
if (b === 0) {
return 'Error: Division by zero is not allowed';
}
return a / b;
}
// Prompt user for input and operation
rl.question('Enter the first number: ', (firstInput) => {
const firstNumber = parseFloat(firstInput);
rl.question('Enter the second number: ', (secondInput) => {
const secondNumber = parseFloat(secondInput);

rl.question('Choose an operation (+, -, *, /): ', (operation) => {


let result;
switch (operation) {
case '+':
result = add(firstNumber, secondNumber);
break;
case '-':
result = subtract(firstNumber, secondNumber);
break;
case '*':
result = multiply(firstNumber, secondNumber);
break;
case '/':
result = divide(firstNumber, secondNumber);
break;
default:
result = 'Invalid operation selected';
}
console.log(`Result: ${result}`);
rl.close();
});
});
});
Step 3: Run the Application
node calculator.js
OUTPUT:
//screen represents the project folder creation and files.
//Execution in Terminal
PS C:\Users\KE0041-IT\Documents\calculatorapp> npm init -y
Wrote to C:\Users\KE0041-IT\Documents\calculatorapp\package.json:
{
"name": "calculatorapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
PS C:\Users\KE0041-IT\Documents\calculatorapp> node calculator.js
Enter the first number: 10
Enter the second number: 20
Choose an operation (+, -, *, /): +
Result: 30
PS C:\Users\KE0041-IT\Documents\calculatorapp> node calculator.js
Enter the first number: 20
Enter the second number: 5
Choose an operation (+, -, *, /): *
Result: 100
PS C:\Users\KE0041-IT\Documents\calculatorapp> node calculator.js
Enter the first number: 120
Enter the second number: 6
Choose an operation (+, -, *, /): /
Result: 20

RESULT:
Thus, a simple calculator application using node.js was created and implemented
successfully.

You might also like