In: Computer Science
Question 1
Given two integers x and y, the following recursive definition
determines the greatest common divisor of x and y, write gcd(xy).
Write a recursive function, gcd, that takes two integers as
parameters and returns the greatest common divisor of numbers. Also
write a program to test your function. Write a recursive function,
reverseDigits, that takes an integer as a parameter and returns the
number with the digits reversed. Also write a program to test your
application.
#include <iostream>
using namespace std;
//Function declaration
int gcd(int x,int y);
int main()
{
//Declaring variables
int x,y,num;
//Getting the first number entered by the user
cout<<"Enter first number :";
cin>>x;
//Getting the second number entered by the user
cout<<"Enter second number :";
cin>>y;
/* Calling the function by passing
* the two numbers as arguments
*/
num=gcd(x,y);
//Displaying the GCD of two numbers
cout<<"The GCD of "<<x<<" and "<<y<<"
is "<<num<<endl;
return 0;
}
/* Function implementation which
* calculates the gcd of two numbers
*/
int gcd(int x,int y)
{
if(y==0)
return x;
else if(y!=0)
return gcd(y,x%y);
}
____________________
Output:
************************************************************************************************************************************
In case of any doubt do ask in the comment section.Hope you like it