Codeforcoding

JavaScript Program to substract Two Numbers

JavaScript Program to subtract Two Numbers

In this tutorial, we will discuss the title of the JavaScript Program to subtract Two Numbers

In this topic, we are going to learn how  to write a program to subtract two numbers in the JavaScript programming language

Subtract of two numbers

JavaScript Program to substract Two Numbers
Subtraction

JavaScript program for subtracting of two numbers

Program 1

Here, We use the minus(-) operator to find the differences between the given two numbers. The difference between the two numbers 24 and 14 and the output is displayed here

//Caculate subtraction of two numbers in JavaScript

const f_Num=24;
const l_Num=14;
//declare and initaiize two variable

//calculate difference of two numbers
const dif=f_Num-l_Num;

//display the difference of two numbers
console.log('The subtraction of '+f_Num+' and '+l_Num+' is: '+dif);

When the above code is executed, it produces the following result

The subtraction of 24 and 14 is: 10

 

JavaScript program for subtracting of two numbers using HTML

Program 2

Here, We use the minus(-) operator to find the differences between two numbers. The difference between two numbers 15 and 20 , the output is displayed here

<html>
<head>
<title>Subtraction of two numbers</title>
<script>
var f_Num=15;
var l_Num=20;
var sub=f_Num-l_Num;
document.write("subtraction of "+f_Num+" and "+l_Num+" is: "+sub);
</script>
</head>
<body>

</body>
</html>

When the above code is executed, it produces the following result

subtraction of 15 and 20 is: -5

 

JavaScript program for subtracting of two numbers using function

Program 3

The program asks the user to enter two numbers using the form and, then result of the difference between two numbers is displayed when the user clicks the “subtract” button

<html>
<head>
<title>subtract two numbers</title>
<script>
function subs()//function to subtraction
{
var f_Num,s_Num,dif;
f_Num=Number(document.getElementById("first").value);
s_Num=Number(document.getElementById("second").value);
dif=f_Num-s_Num;
document.getElementById("ans").innerHTML="Difference:"+dif;
}
</script>
</head>
<body>
<p>Enter the first Number:<input type="text" id="first"></p>
<p>Enter the second Number:<input type="text" id="second"></p>
<button onclick="subs();">Subtract</button>
<p id="ans"></p>
</body>
</html>

When the above code is executed, it produces the following result

Subtract two number

Suggested for you

Subtraction of two numbers in Java language

Subtraction of two numbers in C Language

Subtraction of two numbers in C++ Language

Subtraction of two numbers in Python Language

 

Subtraction of two numbers using method in Java language

Subtraction of two numbers using function in C Language

Subtraction of two numbers using function in C++ Language

Subtraction of two numbers using function in Python Language

Subtraction of two numbers using C# language

Subtraction of two numbers using function in C# language

JavaScript program to addition of two numbers

JavaScript program for adding of two numbers|4 ways

PHP program to add two numbers

PHP program to add two numbers using unction

PHP program to divide two numbers using function
JavaScript: Calculate division of Two Numbers
Exit mobile version