You are given two positive integers X and Y. Repeat the following operation until X is…
- User
You are given two positive integers X and Y.
Repeat the following operation until X is not 0:
If X>Y, swap X and Y; Otherwise, set X=(Y−X) and Y=X.
Output the final value of Y after all operations.
Give c++ solution
Assistant
Here is a C++ solution for the given problem:
#include <iostream> int solve(int x, int y) { while (x != 0) { if (x > y) { int temp = x; x = y; y = temp; } else { int temp = y; y = x; x = temp - x; } } return y; } int main() { int x, y; std::cin >> x >> y; std::cout << solve(x, y) << std::endl; return 0; }This program defines a function
solvethat takes two integersxandyand performs the specified operation untilxbecomes 0. It then returns the final value ofy. Themainfunction reads the input from the user and calls thesolvefunction to get the output.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.