JavaScript Program to find GCD

You are Here:

What is Greatest Common Divisor?

A largest number that exactly divides two or more integers.

In general, Greatest Common Divisor (GCD) is otherwise called as Greatest Common Factor (GCF) or Highest Common Factor (HCF)

Tips: It is recommended to use our online GCD calculator for better understanding.

Examples

The following table provides few examples of GCD of the given numbers.

NumbersGCD
4, 102
6, 51
15, 30 , 4515

GCD of Two Numbers

In the following example, we will find the GCD of the given two numbers (45, 90).

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <h1>JS GCD</h1> <script> var num1 = 45; var num2 = 90; var gcd = 1; var i; for(i=2; i<=num1 && i<=num2; i++) { // Checks if i is factor of both integers if(num1 % i == 0 && num2 % i == 0) gcd = i; } document.write("GCD of "+num1+" and "+num2+" : "+gcd); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author