gcd recursion c++

include

using namespace std;

int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); }

int main() { int num1, num2; cout << "Enter two numbers: "; cin >> num1 >> num2; cout << "GCD of " << num1 << " and " << num2 << " is " << gcd(num1, num2); return 0; }

Explanation: 1. We include the iostream library to use input/output functions in C++. 2. We use the "using namespace std" statement to avoid writing "std::" before standard library functions like cout and cin. 3. We define a function named "gcd" that takes two integers as parameters. 4. In the gcd function, we check if the second integer (b) is equal to 0. If it is, we return the first integer (a). 5. If the second integer is not equal to 0, we recursively call the gcd function with the second integer (b) as the first parameter and the remainder of dividing the first integer (a) by the second integer (b) as the second parameter. 6. In the main function, we declare two integer variables num1 and num2. 7. We prompt the user to enter two numbers by displaying the message "Enter two numbers: ". 8. We use the cin object to read the user's input and store it in the variables num1 and num2. 9. We call the gcd function with num1 and num2 as parameters and print the result using cout. 10. Finally, we return 0 to indicate successful execution of the program.