In: Computer Science
I want a unique c++ code for the following.
(Greatest Common Divisor) Given two integers x and y, the following recursive definition determines the greatest common divisor of x and y, written gcd(x,y): 5 5 ± x y x y y x y y gcd( , ) if 0 gcd( , % ) if 0 Note: In this definition, % is the mod operator. Write a recursive function, gcd, that takes as parameters two integers and returns the greatest common divisor of the numbers. Also, write a program to test your function.
Create a single .cpp file and write your function and your main both in that file.
Use good programming conventions:
Source Code:
Output:
Code in text format (See above images of code for indentation):
#include<iostream>
using namespace std;
/*function definition*/
int gcd(int x,int y)
{
/*if y is 0 then return x*/
if(y==0)
return x;
/*return gcd and recursive call*/
return gcd(y,x%y);
}
/*main function*/
int main()
{
/*variables*/
int x,y,result;
/*read x and y from the user*/
cout<<"Enter x and y values:\n";
cin>>x>>y;
/*function call*/
result=gcd(x,y);
/*print gcd in main function*/
cout<<"The gcd of "<<x<<" and
"<<y<<" is: "<<result;
return 0;
}