JavaScript Program to Perform Simple Mathematical Calculation
Last Updated :
11 Sep, 2023
In this article, we are going to learn how perform simple mathematical calculations by using JavaScript. In this, we will get the two numbers and one operator as input and then the program will calculate the result according to the provided inputs.
Example 1:
Input :
Operation : '+'
num1 : 5
num2 : 6
Output :
11
Explanation:
'+' operator is used to add two values so , 5+6 will be 11.
Example 2:
Input :
Operation : '-'
num1 : 5
num2 : 6
Output :
-1
Explanation:
'-' operator is used to subtract a value from another one so, 5-6 will be -1.
These are the following approaches by using we can make a simple mathematical calculations:
- Using If-Else statement
- Using switch case
Simple Calculations using If-Else Statement in JavaScript
- Create 4 functions for each operation i.e. ‘+’, ‘-‘, ‘*’, ‘/’.
- In the add function add the two numbers using the ‘+’ operator.
- In the subtract function subtract the two numbers using the ‘-‘ operator.
- In the multiply function multiply the two numbers using the ‘*’ operator.
- In the divide function divide the two numbers using the ‘/’ operator.
- Take the operation, both numbers num1 & num2 as input from user.
- Check if the operation is ‘+’ then call the add function. If the operation is ‘-‘ then call the subtract function. If the operation is ‘*’ then call the multiply function. If the operation is ‘/’ then call the divide function.
- Else return Invalid operation if user entered a wrong input.
Example:
Javascript
function add(num1, num2) {
return num1 + num2;
}
function subtract(num1, num2) {
return num1 - num2;
}
function multiply(num1, num2) {
return num1 * num2;
}
function divide(num1, num2) {
if (num2 === 0) return undefined;
return num1 / num2;
}
let num1 = 16;
let num2 = 4;
let operation = "/" ;
let result;
if (operation === "+" ) {
result = add(num1, num2);
} else if (operation === "-" ) {
result = subtract(num1, num2);
} else if (operation === "*" ) {
result = multiply(num1, num2);
} else if (operation === "/" ) {
result = divide(num1, num2);
} else {
result = "Invalid operation" ;
}
console.log( "The Result of this operation is : " + result);
|
Output
The Result of this operation is : 4
Time complexity: O(1)
Space complexity: O(1)
Simple Calculations using Switch Statement in JavaScript
- Create 4 functions for each operation i.e. ‘+’, ‘-‘, ‘*’, ‘/’.
- In the add function add the two numbers using the ‘+’ operator.
- In the subtract function subtract the two numbers using the ‘-‘ operator.
- In the multiply function multiply the two numbers using the ‘*’ operator.
- In the divide function divide the two numbers using the ‘/’ operator.
- Take the operation, both numbers num1 & num2 as input from user.
- Use the Switch Statement and take operation as argument.
- Case 1: if operation is ‘+’ then call the add function.
- Case2: if the operation is ‘-‘ then call the subtract function.
- Case3: if the operation is ‘*’ then call the multiply function.
- Case4: if the operation is ‘/’ then call the divide function.
- Default Case: write ‘Invalid Operation’.
- Print the Result.
Example:
Javascript
function add(num1, num2) {
return num1 + num2;
}
function subtract(num1, num2) {
return num1 - num2;
}
function multiply(num1, num2) {
return num1 * num2;
}
function divide(num1, num2) {
if (num2 === 0) return undefined;
return num1 / num2;
}
let operation = '+' ;
let num1 = 4;
let num2 = 5;
let result;
switch (operation) {
case "+" :
result = add(num1, num2);
break ;
case "-" :
result = subtract(num1, num2);
break ;
case "*" :
result = multiply(num1, num2);
break ;
case "/" :
result = divide(num1, num2);
break ;
default :
result = "Invalid operation" ;
}
console.log( "The Result of this operation is : " + result);
|
Output
The Result of this operation is : 9
Time complexity: O(1)
Space complexity: O(1)
Share your thoughts in the comments
Please Login to comment...