Algorithm
-
Initialize Variables:
- Declare variables to store input values and results.
-
Get User Input:
- Use prompt or other input methods to get user input for the first number, operator, and second number.
-
Validate Input:
- Check if the input values are valid numbers and if the operator is valid (e.g., +, -, *, /).
-
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).
- Use a switch or if-else statements to perform the calculation based on the operator.
-
Display Result:
- Output the result to the user (e.g., using alert, console.log, or updating an HTML element).
-
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
Enter operator ( either +, -, * or / ): *
Enter first number: 3.4
Enter second number: 5.6
3.4 * 5.6 = 19.04
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
Enter operator: +
Enter first number: 4
Enter second number: 5
4 + 5 = 9
Enter first number: 4
Enter second number: 5
4 + 5 = 9
Demonstration
JavaScript Programing Example to Make a Simple Calculator-DevsEnv