Tuesday, December 4, 2012

C++ find common factors


It's such easy to do this:

#include <iostream>
using namespace std;

int gcd(int a, int b)
{
if(b == 0)
{
       return a;
}
else
{
return gcd(b, a % b);
}
}

int main()
{
     int a,b;
         
     cout << "Input first number: ";
     cin >> a;
     cout << "Input second number: ";
     cin >> b;
   
     cout << "Greatest common divisior (GCD) is " << gcd(a,b) << endl;
     return 0;
}

No comments:

Post a Comment