forked from Silver-Taurus/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcd.cpp
60 lines (53 loc) · 1.34 KB
/
gcd.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Given two numbers, determine the greatest common divisior of the two numbers.
*/
#include <iostream>
int gcd1( int a, int b ) {
while( b > 0 ) {
int temp = a % b;
a = b;
b = temp;
}
return a;
}
int gcd2( int a, int b ) {
if ( b == 0 ) return a;
if ( a == 0 ) return b;
return gcd2(b, a % b);
}
int gcd3( int a, int b ) {
if ( a == 0 ) return b;
if ( b == 0 ) return a;
while ( a != b ) {
if ( a > b ) {
a = a-b;
} else {
b = b-a;
}
}
return a;
}
int gcd4 ( int a, int b ) {
if ( a == 0 ) return b;
if ( b == 0 ) return a;
if ( a == b ) return a;
if ( a > b ) return gcd4(b, a-b);
else return gcd4(a, b-a);
}
int main()
{
int a, b;
std::cout << "Enter number 1 : ";
std::cin >> a;
std::cout << "Enter number 2 : ";
std::cin >> b;
std::cout << "GCD of " << a << " and "
<< b << " is " << gcd1(a,b) << std::endl;
std::cout << "GCD of " << a << " and "
<< b << " is " << gcd2(a,b) << std::endl;
std::cout << "GCD of " << a << " and "
<< b << " is " << gcd3(a,b) << std::endl;
std::cout << "GCD of " << a << " and "
<< b << " is " << gcd4(a,b) << std::endl;
return 0;
}