Factorial of number in JavaScript

Advertisements

Prev Tutorial Next Tutorial

Find Factorial of number in JavaScript

Using javascript you can find factorial of any number, here same logic are applied like c language

you only need to write your javascript code inside <script> ..... </script> tag using any editor like notepad or edit plus. Save below code with .html or .htm extension. No need to compile your javascript code just open your code in any web browser.

Find factorial of number using JavaScript

Example Factorial of any number

<!doctype html>
<html>
<head>
<script>
function show(){

var i, no, fact;
fact=1;
no=Number(document.getElementById("num").value);
for(i=1; i<=no; i++)  
{
fact= fact*i;
}  
document.getElementById("answer").value= fact;
}
</script>
</head>
<body>
Enter Num: <input id="num">
<button onclick="show()">Factorial</button>
<input id="answer">
</body>
</html>

Result

Enter Num:

Code Explanation

  • no=Number(document.getElementById("num").value);
    This code is used for receive input value form input field which have id num.
  • document.getElementById("answer").value= fact;
    This code is used for receive calculated value of factorial and display in input field which have id answer
  • <button onclick="show()">Factorial</button>
    This code is used for call show function when button clicked.

Prev Tutorial Next Tutorial

Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects