- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find x and y satisfying ax + by = n in C++

In this problem, we are given three integer values a, b, and n. Our task is to *find x and y satisfying ax + by = n.*

**Let's take an example to understand the problem**

Input : a = 4, b = 1, n = 5 Output : x = 1, y = 1

## Solution Approach

A simple solution to the problem is by finding the value between 0 to n that satisfies the equation. We will do this by using altered forms of the equation.

x = (n - by)/a y = (n- ax)/b

If we get a value satisfying the equation, we will print the values otherwise print "*no solution exists*".

## Example

Program to illustrate the working of our solution

#include <iostream> using namespace std; void findSolution(int a, int b, int n){ for (int i = 0; i * a <= n; i++) { if ((n - (i * a)) % b == 0) { cout<<i<<" and "<<(n - (i * a)) / b; return; } } cout<<"No solution"; } int main(){ int a = 2, b = 3, n = 7; cout<<"The value of x and y for the equation 'ax + by = n' is "; findSolution(a, b, n); return 0; }

## Output

The value of x and y for the equation 'ax + by = n' is 2 and 1

- Related Articles
- Find smallest values of x and y such that ax – by = 0 in C++
- Maximize the value of x + y + z such that ax + by + cz = n in C++
- Solve for $x$ and $y$: $frac{x}{a}=frac{y}{b}; ax+by=a^{2}+b^{2}$.
- If $ax+by=a^{2}-b^{2}$ and $bx+ay=0$, then find the value of $( x+y)$.
- Find larger of x^y and y^x in C++
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++
- If ( a=x^{m+n} y^{l}, b=x^{n+l} y^{m} ) and ( c=x^{l+m} y^{n} ), prove that ( a^{m-n} b^{n-1} c^{l-m}=1 . )
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- If ( x=a^{m+n}, y=a^{n+1} ) and ( z=a^{l+m} ), prove that ( x^{m} y^{n} z^{l}=x^{n} y^{l} z^{m} )
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Find the sum of the following arithmetic progressions:( frac{x-y}{x+y}, frac{3 x-2 y}{x+y}, frac{5 x-3 y}{x+y}, ldots ) to ( n ) terms
- Express the following linear equations in the form $ax + by + c = 0$ and indicate the values of $a, b$ and $c$ in each case:$y = frac{x}{2}$
- Find x and y."

Advertisements