Search

26 December, 2016

Euclid's algorithm for finding Greatest Common divisor of two numbers

/*
*Euclid's algorithm for finding Greatest Common divisor of two numbers
Input: Two integers a and b with a>=b>=0
Output: gcd(a,b)
Date: 26/12/2016
*/

#include<iostream>
#include <stdlib.h>

using namespace std;

int gcd(int a, int b){
    cout << a << endl;
    cout << b << endl;

return b == 0 ? a : gcd(b, a % b);
}

int main(){

int a, b;

cout << "Enter value of a: ";
cin >> a;
cout << "\n";
cout << "Enter value of b: ";
cin >> b;

cout << "GCD OF "<< a << " and " << b << " is: " << gcd(abs(a), abs(b));
cout << "\n";

return 0;
}