- 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 the final X and Y when they are Altering under given condition in C++

Consider we have the initial values of two positive integers X and Y. Find the final value of X and Y, such that there will be some alteration as mentioned below −

step1 − If X = 0 and Y = 0 then terminate the process, otherwise go to step2

step2 − If X >= 2Y, then set X = X – 2Y, and go to step1, otherwise go to step3

step3 − If Y >= 2X, then set Y = Y – 2X, and go to step1, otherwise end the process.

The number X and Y will be in range [0 and 1018] So we can use the Brute Force approach.

## Example

#include<iostream> using namespace std; void alterNumber(long long x, long long y) { while (1) { if (x == 0 || y == 0) break; if (x >= 2 * y) x = x % (2 * y); else if (y >= 2 * x) y = y % (2 * x); else break; } cout << "X: " << x << "

" << "Y: " << y; } int main() { long long x = 12, y = 5; alterNumber(x, y); }

## Output

X: 0 Y: 1

- Related Questions & Answers
- Find the final X and Y when they are Altering under given condition in C++ Programming
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- Find larger of x^y and y^x in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Find Four points such that they form a square whose sides are parallel to x and y axes in Python
- Find Four points such that they form a square whose sides are parallel to x and y axes in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Program to find number of pairs between x, whose multiplication is x and they are coprime in Python
- Program to get final position of moving animals when they stops in Python
- Find duplicates under given constraints in C++
- Find x and y satisfying ax + by = n in C++
- Java program to find the permutation when the values n and r are given
- Find numbers a and b that satisfy the given condition in C++
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++

Advertisements