Find x and y satisfying ax + by = n

08 Jun, 2022
Given a, b and n. Find x and y that satisfies ax + by = n. Print any of the x and y satisfying the equationExamples :  
Input : n=7 a=2 b=3
Output : x=2, y=1 
Explanation: here x and y satisfies the equation

Input : 4 2 7 
Output : No solution
 
We can check if any solutions exists or not using Linear Diophantine Equations, but here we need to find out the solutions for this equation, so we can simply iterate for all possible values from 0 to n as it cannot exceed n for this given equation. So solving this equation with pen and paper gives y=(n-ax)/b and similarly we get the other number to be x=(n-by)/a.If none of the values satisfies the equation, at the end we print “no solution”  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find solution of ax + by = n#include <bits/stdc++.h>using namespace std;  // function to find the solutionvoid solution(int a, int b, int n){    // traverse for all possible values    for (int i = 0; i * a <= n; i++) {          // check if it is satisfying the equation        if ((n - (i * a)) % b == 0) {            cout << "x = " << i << ", y = "                  << (n - (i * a)) / b;            return;        }    }      cout << "No solution";}  // driver program to test the above functionint main(){    int a = 2, b = 3, n = 7;    solution(a, b, n);    return 0;}
// Java program to find solution// of ax + by = nimport java.io.*;  class GfG {              // function to find the solution    static void solution(int a, int b, int n)    {        // traverse for all possible values        for (int i = 0; i * a <= n; i++)        {                  // check if it is satisfying the equation            if ((n - (i * a)) % b == 0)            {                System.out.println("x = " + i +                                    ", y = " +                                    (n - (i * a)) / b);                                  return ;            }        }              System.out.println("No solution");    }                public static void main (String[] args)     {        int a = 2, b = 3, n = 7;        solution(a, b, n);          }}  // This code is contributed by Gitanjali.
# Python3 code to find solution of# ax + by = n  # function to find the solutiondef solution (a, b, n):      # traverse for all possible values    i = 0    while i * a <= n:                  # check if it is satisfying        # the equation        if (n - (i * a)) % b == 0:            print("x = ",i ,", y = ",               int((n - (i * a)) / b))            return 0        i = i + 1          print("No solution")  # driver program to test the above functiona = 2b = 3n = 7solution(a, b, n)  # This code is contributed by "Sharad_Bhardwaj".
// C# program to find solution// of ax + by = nusing System;  class GfG {              // function to find the solution    static void solution(int a, int b, int n)    {                  // traverse for all possible values        for (int i = 0; i * a <= n; i++)        {                  // check if it is satisfying the            // equation            if ((n - (i * a)) % b == 0)            {                Console.Write("x = " + i +                                 ", y = " +                         (n - (i * a)) / b);                                  return ;            }        }              Console.Write("No solution");    }          // Driver code    public static void Main ()     {        int a = 2, b = 3, n = 7;        solution(a, b, n);          }}  // This code is contributed by Vt_m.
<?php// PHP program to find // solution of ax + by = n  // function to find the solutionfunction solution($a, $b, $n){    // traverse for all possible values    for ($i = 0; $i * $a <= $n; $i++)     {          // check if it is satisfying        // the equation        if (($n - ($i * $a)) % $b == 0)         {            echo "x = " , $i , ", y = " ,                   ($n - ($i * $a)) / $b;            return;        }    }      echo "No solution";}  // Driver Code$a = 2; $b = 3; $n = 7;solution($a, $b, $n);  // This code is contributed by anuj_67.?>
<script>  // JavaScript program to find solution// of ax + by = n      // function to find the solution    function solution(a, b, n)    {        // traverse for all possible values        for (let i = 0; i * a <= n; i++)        {                    // check if it is satisfying the equation            if ((n - (i * a)) % b == 0)            {                document.write("x = " + i +                                    ", y = " +                                    (n - (i * a)) / b);                                    return ;            }        }                document.write("No solution");    }  // Driver code          let a = 2, b = 3, n = 7;        solution(a, b, n);                    // This code is contributed by sanjoy_62.</script>
x = 2, y = 1
Time Complexity: O(n/a), as we are using a loop to traverse n/a times.Auxiliary Space: O(1)
vt_m
sanjoy_62
rohitsingh57
Algebra
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.