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
// 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);
OutputThe 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);
OutputThe Result of this operation is : 9
Time complexity: O(1)
Space complexity: O(1)
Similar Reads
JavaScript Program to find simple interest What is âSimple Interestâ?Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments.Simple Interest formula:Simple interest formula is given
2 min read
JavaScript Program for Multiplication of Two Numbers In this article, we are going to learn how we can multiply two numbers using JavaScript. Multiplying the two numbers in JavaScript involves performing arithmetic addition on numeric values, resulting in their sum. JavaScript supports numeric data types. In JavaScript, both floating-point and integer
4 min read
JavaScript Programs JavaScript Programs contains a list of articles based on programming. This article contains a wide collection of programming articles based on Numbers, Maths, Arrays, Strings, etc., that are mostly asked in interviews.Table of ContentJavaScript Basic ProgramsJavaScript Number ProgramsJavaScript Math
13 min read
C Program to Make a Simple Calculator A simple calculator is a program that can perform addition, subtraction, multiplication, and division of two numbers provided as input. In this article, we will learn to create a simple calculator program in C.ExampleInput: a = 10, b = 5, op = +Output: 15.00Explanation: Chosen operation is addition,
3 min read
How to calculate multiplication and division of two numbers using JavaScript ? We will see the calculation such as multiplication and division using Javascript. An arithmetic operation operates on two numbers and numbers are called operands. Approach: Multiplication of two numbersCreate the HTML form to take input from the user to perform multiplication operations. Add javasc
2 min read