# Java Program for Basic Euclidean algorithms

GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common factors.

`// Java program to demonstrate working of extended` `// Euclidean Algorithm` ` ` `import` `java.util.*;` `import` `java.lang.*;` ` ` `class` `GFG {` ` ` `// extended Euclidean Algorithm` ` ` `public` `static` `int` `gcd(` `int` `a, ` `int` `b)` ` ` `{` ` ` `if` `(a == ` `0` `)` ` ` `return` `b;` ` ` ` ` `return` `gcd(b % a, a);` ` ` `}` ` ` ` ` `// Driver Program` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `a = ` `10` `, b = ` `15` `, g;` ` ` `g = gcd(a, b);` ` ` `System.out.println(` `"GCD("` `+ a + ` `", "` `+ b + ` `") = "` `+ g);` ` ` ` ` `a = ` `35` `;` ` ` `b = ` `10` `;` ` ` `g = gcd(a, b);` ` ` `System.out.println(` `"GCD("` `+ a + ` `", "` `+ b + ` `") = "` `+ g);` ` ` ` ` `a = ` `31` `;` ` ` `b = ` `2` `;` ` ` `g = gcd(a, b);` ` ` `System.out.println(` `"GCD("` `+ a + ` `", "` `+ b + ` `") = "` `+ g);` ` ` `}` `}` `// Code Contributed by Mohit Gupta_OMG <(0_o)>` |

**Output:**

GCD(10, 15) = 5 GCD(35, 10) = 5 GCD(31, 2) = 1

**Time Complexity:** O(Log min(a, b))

Please refer complete article on Basic and Extended Euclidean algorithms for more details!