calculations

JavaScript Program for subtracting Two Numbers | 4 different ways

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 >

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

Output 3

 

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 >

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

After subtraction

Output 4

 

After clear

Output 5

 

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 in C#

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

Addition two number  in C++ language

Addition two number in Python language

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.