Table of Contents
JavaScript program for dividing two numbers| in 4 different ways
In this article, we will discuss the concept of the JavaScript program for dividing two numbers| in 4 different ways
In this post, we are going to learn how to write a program to find the division of two numbers and display the result on the screen in JS language
Code to find the division of two numbers in JS
Division of two numbers in JS
In this program, the user initiates the values to variables f_Num and l_Num, the program calculates the division of the given numbers using the division(/) operator in JavaScript.
Program 1
//JavaScript program to find division of two numbers const fNum=75; //declare and initaiize variable fNum const lNum=5; //declare and initaiize variable lNum //calculate division of two numbers const divNum=fNum/lNum; //display the division of two numbers console.log('The division of '+fNum+' and '+lNum+' is: '+divNum); //using concatenate operator
When the above code is executed, it produces the following result
The division of 75 and 5 is: 15
Division of two numbers in JS – using HTML
In this program, the user initiates the values to variables fNum and lNum, The program calculates the division of the given numbers using the division(/) operator inside the HTML tag
Program 2
<html> <head> <title>Dividing two numbers</title> <script> var fNum=35; var lNum=5; //Decare and initiaize variabes fNum and lNum var divRes=fNum/lNum; //Calculate division of two numbers //and assign the value to divRes variable document.write("Division of "+fNum+" and "+lNum+" is: "+divRes); //Print result on the screen using divRes variable </script> </head> <body> </body> </html>
When the above code is executed, it produces the following result
Division of 35 and 5 is: 7
Division of two numbers in JS – Entered by the user
In this program, the user is asked to enter values to variables f_Num and l_Num, the program calculates the division of the given numbers using the division(/) operator using the form.
Program 1
<html> <head> <title>Dividing two numbers</title> <script> //function to find division of two numbers function div() { var fNum,lNum,division; fNum=Number(document.getElementById("first").value); sNum=Number(document.getElementById("second").value); //Reading the values divRes=fNum/sNum; //Calculate division of two numbers document.getElementById("ans").innerHTML="Division:"+divRes; //display result on the screen } </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
Division of two numbers in JS – Entered by the user
In this program, the user is asked to enter values to variables f_Num and l_Num using the form, the program calculates the division of the given numbers using the division(/) operator, and then when we click the “clear” button, entered fields are cleared
Program 1
<html> <head> <title>Dividing two numbers</title> <script> //function to find division of two numbers function div() { var fNum,lNum,division; fNum=Number(document.getElementById("first").value); sNum=Number(document.getElementById("second").value); //Reading the values divRes=fNum/sNum; //Calculate division of two numbers document.getElementById("ans").innerHTML="Division:"+divRes; //display result on the screen } 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 Number:<input type="text" id="first"></p> <p>Enter the second Number:<input type="text" id="second"></p> <button onclick="div();">division</button> <button onclick="clr();">clear</button> <p id="ans"></p> </body> </html>
When the above code is executed, it produces the following result
Suggested post
Java program to divide two number
C program to divide two number
C++ program to divide two number
Python program to divide two number
java program to find the quotient and remainder
C program to find quotient and remainder
C++ program to find quotient and remainder
Python program to find quotient and remainder