forked from ambujraj/hacktoberfest2018
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcd.cpp
More file actions
30 lines (27 loc) · 672 Bytes
/
gcd.cpp
File metadata and controls
30 lines (27 loc) · 672 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
#include<bits/stdc++.h>
using namespace std;
int recursive_gcd(int a, int b) { // DIVIDEND = a DIVISOR = b
if(b==0)
return a;
return recursive_gcd(b,a%b); // DIVIDEND BECOMES 'b' AND DIVISOR BECOMES 'remainder'
}
int iterative_gcd(int a,int b) {
int divi = a;
int divisor = b;
while(divisor != 0) {
int rem = divi % divisor;
divi = divisor;
divisor = rem;
}
return divi;
}
int main()
{
int a,b;
cin>>a>>b;
int c = recursive_gcd(a,b);
cout<<c<<"\n";
c = iterative_gcd(a,b);
cout<<c<<"\n";
return 0;
}