»
EnglishFrenchVietnamese

Print - GCD/LCM Calculator - JavaScriptBank.com

Full version: jsB@nk » Calculation » Math » GCD/LCM Calculator
URL: https://www.javascriptbank.com/gcd-lcm-calculator.html

GCD/LCM Calculator © JavaScriptBank.comThis calculator code can quickly determine the Greatest Common Factor and Least Common Multiplier between two or three numbers.

Full version: jsB@nk » Calculation » Math » GCD/LCM Calculator
URL: https://www.javascriptbank.com/gcd-lcm-calculator.html



JavaScript
<script type="text/javascript"><!-- Original: Seamus Yim --><!-- Beginfunction go(input1,input2,input3){  if (document.form1.text3.value!=""){    document.form1.answer.value=hcf(hcf(input1,input2),input3);    document.form1.answer2.value=lcm(lcm(input1,input2),input3);  } else {    document.form1.answer.value=hcf(input1,input2);    document.form1.answer2.value=lcm(input1,input2);  }}function hcf(text1,text2){  var gcd=1;  if (text1>text2) {text1=text1+text2; text2=text1-text2; text1=text1-text2;}  if ((text2==(Math.round(text2/text1))*text1)) {gcd=text1}else {   for (var i = Math.round(text1/2) ; i > 1; i=i-1) {    if ((text1==(Math.round(text1/i))*i))     if ((text2==(Math.round(text2/i))*i)) {gcd=i; i=-1;}   }  }  return gcd;}function lcm(t1,t2){  var cm=1;  var f=hcf(t1,t2);  cm=t1*t2/f;  return cm;}//  End --></script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<form name="form1">First Number:  <input type="text" name="text1" size="3">  Second Number:  <input type="text" name="text2" size="3"><br>  Third Number (<em>not required</em>):  <input type="text" name="text3" size="3"><br><br><input type="button" value="Find GCF and LCM" onclick="go(eval(document.form1.text1.value),eval(document.form1.text2.value),eval(document.form1.text3.value))"><br><br>Greatest Common Factor (GCF):   <input type="text" name="answer" size="3" readonly><br>Least Common Multiplier (LCM):   <input type="text" name="answer2" size="3" readonly></form><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->