aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301/wk3/lab3/gcd.cpp
blob: 4e127d9c5a0595bdfe13373d0d89c76275bd3709 (plain) (blame)
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
#include <iostream>
#include <cmath>
using namespace std;

int GCD(int a, int b) {
  int gcd=1;
  a=abs(a); b=abs(b);
  if (a>b) {
    //do nothing
  } else {
    if (b>a) {
     int foo=a; a=b; b=foo;
   } else {
      return -1;
    }
  }
  gcd=(a%b);
  while (gcd!=0) {
    return GCD(b,gcd);
  }
  return b;

}
int main () {
  int a=0, b=0;
  char cont;
  do {
    cout << "enter two integer values: ";
    cin >> a >> b;
    cout << "greatest common divisor is: " << GCD(a,b) << endl;
    cout << "continue? (y/n): ";
    cin >> cont;
    cout << endl;
  } while (cont!='n');
  
}