News In A Snap!!

C++ Programs: Find GCD of Two Numbers

June 2, 2015


What is GCD?

In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.


// Program to find GCD of two numbers



int gcd(int m, int n, int d=-1) {

if (d==-1)

d = m>n ? n : m;

if (m%d==0 && n%d==0)

return d;

else return

gcd(m, n, d-1);


int main() {


int m, n;

cout << "Enter first number: ";

cin >> m;

cout << "Enter second number: ";

cin >> n;

cout << "GCD is " << gcd(m,n);


return 0;



gcd | Get Tech Addicted...
About Us | Contact Us | ©2017