Codeforcoding

JavaScript: Calculate division of Two Numbers

JavaScript: Calculate the division of Two Numbers

In this tutorial, we will discuss the title of the JavaScript: Calculate division of Two Numbers

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

Division of two numbers

Division of two num

JavaScript program for dividing two numbers

Program 1

Here, We use the division(/) operator to find the division of the given two numbers.

The program calculates the division of the two numbers 70 and 5; the output is displayed here.

// JavaScript program to division of two number
const f_Num=70;
//declare and initaiize variable f_Num
const l_Num=5;
//declare and initaiize variable l_Num

//calculate division of given two numbers
const div=f_Num/l_Num;

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

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

The division of 70 and 5 is: 14

 

JavaScript program for dividing two numbers using HTML

Program 2

Here, We use the division(/) operator to find the division of two numbers. The program calculates the division of two numbers 30 and 5 then the output is displayed here using HTML

<html>
<head>
<title>Division of two numbers</title>
<script>
var fNumber=30;
var lNumber=5;
//Decare and initiaize variabes
var div=fNumber/lNumber;
//Calculate division of the given numbers 
//and assign the value to div variable
document.write("Division of "+fNumber+" and "+lNumber+" is: "+div);
//print result on the screen using div variable
</script>
</head>
<body>

</body>
</html>

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

Division of 30 and 5 is: 6

 

 

JavaScript program for dividing two numbers using the function

Program 3

The program asks the user to enter two numbers using the form, then the result of the division of two numbers is displayed when the user clicks the “division” button.

<html>
<head>
<title>Division of two numbers</title>
<script>
function div()//function to find division of given numbers
{
var fNum,lNum,division;
fNum=Number(document.getElementById("first").value);
sNum=Number(document.getElementById("second").value);
division=fNum/sNum;
document.getElementById("ans").innerHTML="Division:"+division;
}

</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="div();">division</button>
<p id="ans"></p>
</body>
</html>

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

Outpt

 

 

Similar post

Java program to divide two numbers

C program to divide two numbers

C++ program to divide two numbers

Python program to divide two numbers

 

Java program to divide two floating-point  numbers

C program to divide two floating-point numbers

C++ program to divide two floating-point numbers

Python program to divide two floating-point numbers

 

 

 

 

JavaScript Program to substract Two Numbers
Program to convert Celsius into Fahrenheit in Java
Exit mobile version