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
#include <iostream> int main() { int X, Y; std::cout << "Enter two positive integers X and Y: "; std::cin >> X >> Y; while (X != 0) { if (X > Y) { std::swap(X, Y); } else { X = Y - X; Y = X; } } std::cout << "Final value of Y: " << Y << std::endl; return 0; }This C++ program takes two positive integers X and Y as input, performs the given operation until X becomes 0, and then outputs the final value of Y. The
std::swapfunction is used to swap X and Y when X is greater than Y, and the assignment statements are used to update X and Y otherwise.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.