Two ways of solving greatest common divisor: recursion and iteration.
#include <algorithm> using namespace std; int maxCommonDivisor1(int a, int b) { int n = max(a, b), d = min(a, b), m = n % d; return (m==0)? d: maxCommonDivisor1(d, m); } int maxCommonDivisor2(int a, int b) { int n = max(a, b), d = min(a, b), m = n % d; while (m != 0) { n = d; d = m; m = n % d; } return d; }