there is was a question that i answered before and now it`s done by recursive for who needs:)


2. Write a recursive function GCD that returns the greatest common divisor of two integers. Where GCD is the largest integer that evenly divides each of the numbers.
Sample output
Enter two numbers: 21 14
GCD is: 7


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std; 

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

int main()
{
     int a,b;
          
     cout << "enter two numbers: ";
     cin >> a>>b;

     cout << "GCD is: " << GCD(a,b) << endl;
     return 0;
}


/*
enter two numbers: 21 14
GCD is: 7
Press any key to continue
*/
Topic archived. No new replies allowed.