Table of Contents
JavaScript Program for subtracting Two Numbers | 4 different ways
In this article, we will discuss the concept of JavaScript Program for Subtracting Two Numbers
In this post, we are going to learn how to write a program to find subtraction of two numbers and dispay result on the screen in JS language
Code to find difference of two numbers in JS
Subraction of two numbers in JS
In this program, the user initiates the values to variables f_Num and l_Num then calculates the difference of the given numbers using minus(-) operator
Program 1
const f_Num=17; //declare and initaiize variable for first num const l_Num=13; //declare and initaiize variable for second num //calculate difference of given two numbers const diff=f_Num-l_Num; //display the difference of two numbers console.log('The difference of '+f_Num+' and '+l_Num+' is: '+diff);
When the above code is executed, it produces the following result
The difference of 17 and 13 is: 4
Subtraction of two numbers in JS using HTML
In this program, the user initiates the values to variables firstNum and lastNum then calculates the difference of the given numbers using minus(-) operator inside the HTML tag
Program 1
<html> <head> <title>Subtraction of two numbers</title> <script> var firstNum=32; var lastNum=20;//Decare and initiaize variabes var sub=firstNum-lastNum;//Calculate difference of given numbers document.write("subtraction of "+firstNum+" and "+lastNum+" is: "+sub); //print result on the screen </script> </head> <body> </body> </html>
When the above code is executed, it produces the following result
subtraction of 32 and 20 is: 12
Subtraction of two numbers in JS using function – #1
In this program, the program is asked to enter two numbers from the user and calculates the difference of the given numbers using the function in JavaScript language
Program 3
<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
Subtraction of two numbers in JS using function – #2
In this program, the program is asked to enter two numbers from the user and calculates the difference of the given numbers using the function in JavaScript language
Program 4
<html> <head> <title>Difference of two numbers</title> <script> function sub()//function for 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; } function clr()//function to clear all fields { document.getElementById("first").value=""; document.getElementById("second").value=""; document.getElementById("ans").innerHTML=""; } </script> </head> <body> <p>Enter the first value:<input type="text" id="first"></p> <p>Enter the second value:<input type="text" id="second"></p> <button onclick="sub();">Subtract</button> <button onclick="clr();">clear</button> <p id="ans"></p> </body> </html>
When the above code is executed, it produces the following result
After subtraction
After clear
Suggested post
Subtract two number in Java language
Subtract two number in C language
Subtract two number in C++ language
Subtract two number in Python language
Sum of two integers using function in C#
Sum of two floating point numbers in C#
Addition two number in Java language
Addition two number in C language