Algorithm


  1. Initialize Variables:

    • Declare variables to store input values and results.
  2. Get User Input:

    • Use prompt or other input methods to get user input for the first number, operator, and second number.
  3. Validate Input:

    • Check if the input values are valid numbers and if the operator is valid (e.g., +, -, *, /).
  4. Perform Calculation:

    • Use a switch or if-else statements to perform the calculation based on the operator.
      • Addition: Add the two numbers.
      • Subtraction: Subtract the second number from the first.
      • Multiplication: Multiply the two numbers.
      • Division: Divide the first number by the second (make sure the second number is not zero).
  5. Display Result:

    • Output the result to the user (e.g., using alert, console.log, or updating an HTML element).
  6. Repeat or Exit:

    • Ask the user if they want to perform another calculation.
    • If yes, go back to step 2.
    • If no, exit the program.

 

Code Examples

#1 Code Example- Simple Calculator with if..else if...else

Code - Javascript Programming

// program for a simple calculator

// take the operator input
const operator = prompt('Enter operator ( either +, -, * or / ): ');

// take the operand input
const number1 = parseFloat(prompt('Enter first number: '));
const number2 = parseFloat(prompt('Enter second number: '));

let result;

// using if...else if... else
if (operator == '+') {
    result = number1 + number2;
}
else if (operator == '-') {
    result = number1 - number2;
}
else if (operator == '*') {
    result = number1 * number2;
}
else {
    result = number1 / number2;
}

// display the result
console.log(`${number1} ${operator} ${number2} = ${result}`);
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter operator ( either +, -, * or / ): *
Enter first number: 3.4
Enter second number: 5.6
3.4 * 5.6 = 19.04

#2 Code Example- Simple Calculator with switch

Code - Javascript Programming

// program for a simple calculator
let result;

// take the operator input
const operator = prompt('Enter operator ( either +, -, * or / ): ');

// take the operand input
const number1 = parseFloat(prompt('Enter first number: '));
const number2 = parseFloat(prompt('Enter second number: '));

switch(operator) {
    case '+':
         result = number1 + number2;
        console.log(`${number1} + ${number2} = ${result}`);
        break;

    case '-':
         result = number1 - number2;
        console.log(`${number1} - ${number2} = ${result}`);
        break;

    case '*':
         result = number1 * number2;
        console.log(`${number1} * ${number2} = ${result}`);
        break;

    case '/':
         result = number1 / number2;
        console.log(`${number1} / ${number2} = ${result}`);
        break;

    default:
        console.log('Invalid operator');
        break;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter operator: +
Enter first number: 4
Enter second number: 5
4 + 5 = 9
Advertisements

Demonstration


JavaScript Programing Example to Make a Simple Calculator-DevsEnv

Previous
JavaScript Practice Example #3 - Assign 3 Variables and Print Good Way