Saturday, November 21, 2015

Greatest Common Denominator in JavaScript


A simple program that I wrote in JavaScript that will compute the Greatest Common Denominator. The code is very simple and easy to understand.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.



Sample Program Output


Program Listing


<html>
 <head>
   <title> Greatest Common Denominator </title>
  </head>
  <style>
  h3 {
     font-family:arial;
 </style>
 <body> 
 <h3> Greatest Common Denominator </h3>
 <script>

 function GCD(nums)
{
        if(!nums.length)
                return 0;
        for(var r, a, i = nums.length - 1, GCDNum = nums[i]; i;)
                for(a = nums[--i]; r = a % GCDNum; a = GCDNum, GCDNum = r);
        return GCDNum;
}

 val_1 = Number(prompt("Enter First Number  : "));
 val_2 = Number(prompt("Enter Second Number : "));
 val_3 = Number(prompt("Enter Third Number  : "));
 
document.write("<font face='arial' face='14'> <b>");
document.write("The Greatest Common Denomination of " + val_1 + "," + val_2 
 + " and " +val_3 + " is " + GCD([val_1, val_2, val_3])+". <b></font>"); 
</script>
</body>
</html>

 

No comments:

Post a Comment