C++ program to find GCD of two or more numbers using variadic functions














































C++ program to find GCD of two or more numbers using variadic functions



/* C++ program to implement euclidean algorithm to find * greatest common divisor of two or more number using variadic functions */ #include <iostream> using namespace std; template<typename P> P gcd(P a) { return a; } template<typename T> T gcd(T a, T b) { T c; while (b) { c = b; b = a % b; a = c; } return a; } template<typename U, typename... Args> U gcd(U a, Args... args){ return gcd(a, gcd(args...)); } int main() { cout << "GCD of 4, 8 and 12 is: " << gcd(4, 8, 12) << endl; cout << "GCD of 49 and 21 is: " << gcd(49, 21) << endl; cout << "GCD of 36, 45, 90 and 7 is: " << gcd(36, 45, 90, 7) << endl; return 0; }

Output of above program is:


Comments