Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Program to Perform Simple Mathematical Calculation

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:

  • 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




// 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

  • 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




// 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)

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments