Open In App

JavaScript Program to Perform Simple Mathematical Calculation

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:



Simple Calculations using If-Else Statement in JavaScript

Example:




// JavaScript program for 
// simple mathematical calculations
  
// Add two numbers
function add(num1, num2) {
    return num1 + num2;
}
  
// Function for subtraction
function subtract(num1, num2) {
    return num1 - num2;
}
  
// For multiplying of two numbers
function multiply(num1, num2) {
    return num1 * num2;
}
  
// Function for division of
// two numbers
function divide(num1, num2) {
if(num2 === 0) return undefined;
    return num1 / num2;
}
  
// Creating variables for
// num1 and num2
let num1 = 16;
let num2 = 4;
  
let operation = "/";
  
// Creating result variable
let result;
  
// If-Else conditions
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";
}
// Printing the final result
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

Example:




// JavaScript program for 
// simple mathematical calculations
  
// Add two numbers
function add(num1, num2) {
    return num1 + num2;
}
// Function for subtraction
function subtract(num1, num2) {
    return num1 - num2;
}
  
// For multiplying of two numbers
function multiply(num1, num2) {
    return num1 * num2;
}
// Function for division of
// two numbers
function divide(num1, num2) {
if(num2 === 0) return undefined;
    return num1 / num2;
}
// Creating variable 
// for operation
let operation = '+';
  
// Creating variables for
// num1 and num2
let num1 = 4;
let num2 = 5;
  
// Creating result variable
let result;
  
// Switch case statement
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";
}
// Printing the final result
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)


Article Tags :