forked from cs-308/group_2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcd.cpp
More file actions
45 lines (38 loc) · 791 Bytes
/
gcd.cpp
File metadata and controls
45 lines (38 loc) · 791 Bytes
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
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
// Function to return gcd of a and b
int gcd(int a, int b)
{
int result = min(a, b); // Find Minimum of a and b
while (result > 0) {
if (a % result == 0 && b % result == 0) {
break;
}
result--;
}
return result; // return gcd of a nd b
}
int gcd_rec(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)
return gcd_rec(a-b, b);
return gcd_rec(a, b-a);
}
// Driver program to test above function
int main()
{
int a,b;
cin>>a>>b;
cout << "GCD of " << a << " and " << b << " is "
<< gcd_rec(a, b);
return 0;
}