Find initial integral solution of Linear Diophantine equation if finite solution exists | Java program for the above approach ; Function to implement the extended euclid algorithm ; Base Case ; Recursively find the gcd ; Function to print the solutions of the given equations ax + by = c ; Condition for infinite solutions ; Condition for no solutions exist ; Condition for no solutions exist ; Print the solution ; Driver Code ; Given coefficients ; Function Call
class GFG { static int x , y ; static int gcd_extend ( int a , int b ) { if ( b == 0 ) { x = 1 ; y = 0 ; return a ; } else { int g = gcd_extend ( b , a % b ) ; int x1 = x , y1 = y ; x = y1 ; y = x1 - ( a / b ) * y1 ; return g ; } } static void print_solution ( int a , int b , int c ) { if ( a == 0 && b == 0 ) { if ( c == 0 ) { System . out . print ( " Infinite ▁ Solutions ▁ " + " Exist " + "NEW_LINE"); } else { System . out . print ( " No ▁ Solution ▁ exists " + "NEW_LINE"); } } int gcd = gcd_extend ( a , b ) ; if ( c % gcd != 0 ) { System . out . print ( " No ▁ Solution ▁ exists " + "NEW_LINE"); } else { System . out . print ( " x ▁ = ▁ " + x * ( c / gcd ) + " , ▁ y ▁ = ▁ " + y * ( c / gcd ) + "NEW_LINE"); } } public static void main ( String [ ] args ) { int a , b , c ; a = 4 ; b = 18 ; c = 10 ; print_solution ( a , b , c ) ; } }