Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
JavaScript program for dividing two numbers|4 difference ways

JavaScript program for dividing two numbers|4 difference ways

Posted on November 26, 2022

Table of Contents

  • JavaScript program for dividing two numbers| in 4 different ways
    • Code to find the division  of two numbers in JS
      • Division of two numbers in JS
      • Division of two numbers in JS – using HTML
      • Division of two numbers in JS – Entered by the user
      • Division of two numbers in JS – Entered by the user

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

JavaScript program for dividing two numbers|4 difference ways
Output

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

JavaScript program for dividing two numbers|4 difference ways

 

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 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes