

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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++
- 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++
- 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++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array in C++
- Sum of first N natural numbers which are divisible by X or Y
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +\ny*y < n in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Find the Number of Solutions of n = x + n x using C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Find the Number of solutions for the equation x + y + z <= n using C++
Advertisements