C++ examples for Data Structure:Algorithm
Recursively find the gcd of two numbers
#include <iostream> int gcd(int, int); int main(int argc, const char *argv[]) { int x, y;/*from w w w . j a v a 2 s. co m*/ std::cout << "Enter x and y: "; std::cin >> x >> y; // tmp must be larger for recursive approach if (x < y) { int tmp = x; x = y; y = tmp; } std::cout << "GCD of " << x << " and " << y << " is " << gcd(x, y) << std::endl; return 0; } // recursive greatest common divisor int gcd(int x, int y) { if (y == 0) { return x; } else { return gcd(y, x % y); } }