JavaScript Program for multiplying Two Numbers | 4 different ways
Multiplication of two numbers:
Table of Contents
JavaScript Program for multiplying Two Numbers | 4 different ways
In this article, we will discuss the concept of the JavaScript Program for multiplying Two Numbers
In this post, we are going to learn how to write a program to find the multiplication of two numbers and display the result on the screen in JS language.
Code to find the multiplication of two numbers in JS
Multiplication of two numbers in JS
In this program, the user initiates the values to variables f_Num and l_Num. The program calculates the multiplication of the given numbers using the multiplying(*) operator.
Program 1
// java script program to multiply of two numbers
const f_Num=6;
//declare and initaiize variable for f_Num
const l_Num=5;
//declare and initaiize variable for l_Num
//calculate product of given two numbers
const product=f_Num*l_Num;
//display the product of two numbers
console.log('The product of '+f_Num+' and '+l_Num+' is: '+product);
When the above code is executed, it produces the following result
The product of 6 and 5 is: 30
Multiplication of two numbers in JS using HTML
In this program, the user initiates the values to variables “firstNum” and “lastNum” and then the program calculates the multiplication of the given numbers using multiplying(*) operator inside the HTML tag
Program 2
<html>
<head>
<title>product of two numbers</title>
<script>
var fNumber=23;
var lNumber=5;
//Decare and initiaize variabes
var mul=fNumber*lNumber;
//Calculate multiplication of the given numbers
//and assign the value to mul variable
document.write("Product of "+fNumber+" and "+lNumber+" is: "+mul);
//print result on the screen using mul variable
</script>
</head>
<body>
</body>
</html>
When the above code is executed, it produces the following result
Product of 23 and 5 is: 115
Multiplication of two numbers in JS using function – #1
In this program, the user is asked to enter two numbers, The program calculates the multiplication of the given numbers using the function in JavaScript language.
Program 3
<html>
<head>
<title>product of two numbers</title>
<script>
function mul()//function to find product of given numbers
{
var fNum,lNum,product;
fNum=Number(document.getElementById("first").value);
sNum=Number(document.getElementById("second").value);
product=fNum*sNum;
document.getElementById("ans").innerHTML="Product:"+product;
}
</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
Multiplication of two numbers in JS using function – #2
In this program, the user is asked to enter two numbers. The program calculates the multiplication of the given numbers using the function in JavaScript language, and then when the user click the “clear” button, entered fields are cleared.
Program 4
<html>
<head>
<title>product of two numbers</title>
<script>
function mul()//function to find product of given numbers
{
var fNum,lNum,product;
fNum=Number(document.getElementById("first").value);
sNum=Number(document.getElementById("second").value);
product=fNum*sNum;
document.getElementById("ans").innerHTML="Product:"+product;
}
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 >
When the above code is executed, it produces the following result