• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Math Related / GCD/LCM Calculator

GCD/LCM Calculator

GCD/LCM Calculator

This calculator can quickly determine the Greatest Common Factor and Least Common Multiplier between two or three numbers.

First Number:     Second Number:  
  Third Number (not required):  



Greatest Common Factor (GCF):   
Least Common Multiplier (LCM):   
  1. Copy the coding into the HEAD of your HTML document
  2. Add the last code into the BODY of your HTML document
<script type=”text/javascript”>
<!– This script and many more are available free online at –>
<!– The JavaScript Source!! http://javascriptsource.com –>
<!– Original: Seamus Yim –>

<!– Begin
function 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>
<div align=”center”>
<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>
</div>

<p><center>
<font face=”arial, helvetica” size”-2″>Free JavaScripts provided<br>
by <a href=”https://javascriptsource.com”>The JavaScript Source</a></font>
</center><p>

Math Related

Related Snippets:

  • Distance Speed Time
  • Round a number to a specified amount of digits
  • Generate a random number in a given range with JavaScript
  • The JavaScript function Math.pow()

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers