Java Program for Extended Euclidean algorithms Last Updated : 20 Feb, 2023 Improve Improve Like Article Like Save Share Report 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 // Java program to demonstrate working of extended // Euclidean Algorithm import java.util.*; import java.lang.*; class GFG { static public void gcdExtended(long a, long b) { long x = 0, y = 1, lastx = 1, lasty = 0, temp; while (b != 0) { long q = a / b; long r = a % b; a = b; b = r; temp = x; x = lastx - q * x; lastx = temp; temp = y; y = lasty - q * y; lasty = temp; } System.out.println("GCD "+a+" and its Roots x : "+ lastx +" y :"+ lasty); } // Driver Program public static void main(String[] args) { long a = 35, b = 15; //this will print result like //Roots x : 1 y :-2 gcdExtended(a, b); } } // Code Contributed by Aryan itsmearyan <(0-o)> Output: gcd(35, 15) = 5 Please refer complete article on Basic and Extended Euclidean algorithms for more details! Like Article Suggest improvement Previous Java Program for Print Number series without using any loop Next Java Program for Zeckendorf\'s Theorem (Non-Neighbouring Fibonacci Representation) Share your thoughts in the comments Add Your Comment Please Login to comment...