- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathGCD_Rec.cpp
29 lines (26 loc) · 483 Bytes
/
GCD_Rec.cpp
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
// C++ program to find GCD of two numbers
#include<iostream>
usingnamespacestd;
// Recursive function to return gcd of a and b
intgcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
returngcd(a-b, b);
returngcd(a, b-a);
}
// Driver program to test above function
intmain()
{
int a = 98, b = 56;
cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);
return0;
}