SnapTimes

News In A Snap!!

C++ Programs: Find GCD of Two Numbers

June 2, 2015

C++_Main

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.

Code


// Program to find GCD of two numbers

#include<iostream.h>

#include<conio.h>

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() {

clrscr();

int m, n;

cout << "Enter first number: ";

cin >> m;

cout << "Enter second number: ";

cin >> n;

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

getch();

return 0;

}

Output

gcd


SnapTimes.in | Get Tech Addicted...
About Us | Contact Us | ©2017