Given a, b and n. Find x and y that satisfies ax + by = n. Print any of the x and y satisfying the equation
Examples :
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++
// CPP program to find solution of ax + by = n #include <bits/stdc++.h> using namespace std; // function to find the solution 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) { cout << "x = " << i << ", y = " << (n - (i * a)) / b; return; } } cout << "No solution"; } // driver program to test the above function int main() { int a = 2, b = 3, n = 7; solution(a, b, n); return 0; } |
Java
// Java program to find solution // of ax + by = n import 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
# Python3 code to find solution of # ax + by = n # function to find the solution def 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 function a = 2b = 3n = 7solution(a, b, n) # This code is contributed by "Sharad_Bhardwaj". |
C#
// C# program to find solution // of ax + by = n using 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 // PHP program to find // solution of ax + by = n // function to find the solution function 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. ?> |
Output :
x = 2, y = 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Sum of minimum value of x and y satisfying the equation ax + by = c
- Generate N integers satisfying the given conditions
- Count of pairs satisfying the given condition
- Smallest positive integer X satisfying the given equation
- Check if elements of an array can be arranged satisfying the given condition
- Count valid pairs in the array satisfying given conditions
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Find the value of f(n) / f(r) * f(n-r)
- Find K such that |A - K| = |B - K|
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find N from the value of N!
- Find 2^(2^A) % B
- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Find two numbers whose sum and GCD are given
- Find gcd(a^n, c) where a, n and c can vary from 1 to 10^9
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m

