Javascript GCD / HCF Calculator

GCD/HCF calculator is used to find the greatest common divisor or the largest positive integer that divides the numbers without a remainder of given numbers.

Features

  • Used to find the greatest common divisor of two or more numbers.
  • Handy tool to determine the highest common factor of given numbers.
  • The calculator works fine for positive integers.

Preview

HIGHEST COMMON FACTOR
Number of Values :



©h

Downloads

Script Code


<!-- Script by hscripts.com -->

<!-- More scripts @www.hscripts.com -->

<html>
<head>
<title>GCD / HCF / GCF Calculator script, Greatest Common Divisor - Free Javascript</title>
<script type="text/javascript">
function calculate() {
var tot=document.getElementById("tot").value;
if (tot=='') {
document.getElementById("msg").innerHTML='Enter Number of Values';
}
else
{

document.getElementById("msg").innerHTML='';
var count=2;
if(tot>1){
var n1=document.getElementById("txt0").value;
var n2=document.getElementById("txt1").value;
var res1=n1*n2;
n1=calc(n1,n2);
while(count<tot){
var gg="txt"+count;
n2=document.getElementById(gg).value;
res1=n1*n2;
n1=calc(n1,n2);
count=count+1;
}
}
if (n1!='') {
document.getElementById("re").style.display="block";
document.getElementById("resul").value=n1;
document.getElementById("msg").innerHTML='';
}
else
{
document.getElementById("msg").innerHTML='Enter Values';
}
}
}
function calc(n1,n2) {
var num1,num2;
if(n1 < n2){
num1=n1;
num2=n2;
}
else{
num1=n2;
num2=n1;
}
var remain=num2%num1;
while(remain>0){
num2=num1;
num1=remain;
remain=num2%num1;
}
return num1;
}
function newtextbox(val)
{
document.getElementById('new1').innerHTML='';
document.getElementById('va').style.display='none';
var tot =document.getElementById('tot').value;
if(isNaN(val))
{
var df=val.substring(0,(val.length-1))
val=df;
}
else{
val=tot;

if(val<6){
var dd=document.getElementById('new1');
var en=document.getElementById('va').style.display='block';
if(tot!=""){
for(var i=0;i<tot;i++)
{
var textbox=document.createElement('input');
textbox.setAttribute("type","text");
var textname="txt"+i;
textbox.setAttribute("id",textname);
textbox.setAttribute('onkeyup','check(this)');
textbox.setAttribute("size","4");
dd.appendChild(textbox);
}
}
}
else{
alert("Enter values upto 5 only");
val='';
}
}
}
function check(ad)
{
var ch=ad.value;
if(isNaN(ch))
{
var df=ch.substring(0,(ch.length-1))
ad.value=df;
}
}
function resetfun() {
document.getElementById('re').style.display='none';document.getElementById('va').style.display='none';
document.getElementById('new1').style.display='none';document.getElementById('tot').value='';document.getElementById("msg").innerHTML='';
document.location.reload();
}
</script>
</head>
<body>
<!-- Script by hscripts.com -->
<table align=center style='border:1px solid green;color: #3D366F;font-size:13px;' cellpadding=2 cellspacing=5>
<form name=frm>
<tr><td>Number of Values:</td><td>
<input type="text" id="tot" name="tot" value="" size="4" onkeyup="newtextbox(this.value),check(this)" maxlength=1>
</td></tr>
<tr><td id=va style='display:none;'>Enter Values:</td><td id='new1'></td>
</tr>
<tr><td align=center colspan=2><input type="button" name=gcf onclick="calculate()" value="GCD/HCF">
<input type='reset' value='Reset' onclick='resetfun()'>
</td></tr>
<tr><td align=right id=re colspan=2 style='display:none;'>GCD of the above numbers:<input type="text" name="resul" id="resul" value="" readonly size="4"></td></tr>
</form>
</table>
<div id='msg' style='color:red;' align='center'></div>
<div align='center' style="font-size: 10px;color: #dadada;" id="dumdiv"><a href="https://www.hscripts.com" id="dum" style="font-size: 10px;color: #dadada;text-decoration:none;color: #dadada;">h</a>
</div>
</body>

</html>



  • Release Date - 30-07-2009
  • Get free version without ©copyright link for just $10/-
  • For customization of this script or any script development, mail to support@hscripts.com

Usage

  • Copy the javascript code into the head part of your HTML page.
  • Here,the function "calculate()" is used to find the gcd/gcf of given numbers.
  • Entering the numbers and a button click completes the calculation of highest common factor.

License

Related Scripts

Free Javascript Scripts


Ask Questions

Ask Question