
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Minimum Positive Integer Value of x for Given a and b in C++
Problem statement
Given values of A and B, find the minimum positive integer value of X that can be achieved in the equation X = P*A + Q*B, Here P and Q can be zero or any positive or negative integer.
Example
If A = 2 and B = 4 then answer will be 2.
Algorithm
- We need to find P and Q such that P*A > P*B and P*A – P*B is minimum positive integer.
- This problem can be easily solved by calculating GCD of both numbers)
Example
#include <iostream> using namespace std; int getGcd(int a, int b) { if (a == 0) { return b; } return getGcd(b % a, a); } int main() { cout << "Answer = " << getGcd(2, 4) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Answer = 2
Advertisements