diff --git a/README.md b/README.md index bc4d0dd..17bc501 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,33 @@ -# JavaLearing -Wnat to add new feature related to 101 +# Simple Addition Program +This is a simple Node.js application that takes two numbers as input and displays their sum in the console. + +## Setup and Installation + +1. Make sure [Node.js](https://nodejs.org/) is installed on your system. +2. Clone or download this repository. +3. Open a terminal and navigate to the project directory. +4. Run `npm install` (no dependencies are required for this project, but this ensures the project is ready). + +## Usage + +1. Run the program using: + + npm start + +2. You will be prompted to enter two numbers. +3. The program will display their sum in the console. + +## Example + + +Simple Addition Program +Enter the first number: 5 +Enter the second number: 10 + +The sum of 5 and 10 is: 15 + + +## License + +This project is licensed under the ISC License. \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..0bce65d --- /dev/null +++ b/index.js @@ -0,0 +1,42 @@ +const readline = require('readline'); + +// Function to add two numbers +function addNumbers(a, b) { + return a + b; +} + +// Create a readline interface for user input +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +// Prompt user for input +console.log("Simple Addition Program"); +rl.question('Enter the first number: ', (firstInput) => { + const num1 = parseFloat(firstInput); + + // Check if the first input is a valid number + if (isNaN(num1)) { + console.log('Invalid input. Please enter a valid number.'); + rl.close(); + return; + } + + rl.question('Enter the second number: ', (secondInput) => { + const num2 = parseFloat(secondInput); + + // Check if the second input is a valid number + if (isNaN(num2)) { + console.log('Invalid input. Please enter a valid number.'); + rl.close(); + return; + } + + // Compute and display the result + const result = addNumbers(num1, num2); + console.log(`\nThe sum of ${num1} and ${num2} is: ${result}`); + + rl.close(); + }); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..54f1b5a --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "addition-program", + "version": "1.0.0", + "description": "A simple addition program that takes input and outputs the sum of two numbers.", + "main": "index.js", + "scripts": { + "start": "node index.js" + }, + "author": "", + "license": "ISC" +} \ No newline at end of file