Wednesday, October 5, 2011

10/5

Recursion program to find the greatest common divisor(update latter)

#include <cstdlib>
#include <iostream>

using namespace std;

int gcd(int a, int b);

int main(int argc, char *argv[])
{
    int larger;
    int smaller;
    int temp;
   
cout << "Hello, "
     << endl;
cout << "This program will calculate the greatest common divisor"
     << endl;


cout << "Please enter a first number number: "
     << endl;
cin >> smaller;
cout << "Please enter a second number: "
     << endl;
cin >> larger;

cout << gcd(smaller, larger) << endl;


   
    system("PAUSE");
    return EXIT_SUCCESS;
}

int gcd(int smaller, int larger)
{   
   
     int temp;
     int rem;
    
     if (smaller > larger)
     {
                 temp = larger;
                 larger = smaller;
                 smaller = temp;
}
     rem = larger % smaller;
     if (rem == 0)
      //if rem >9000....
        return smaller;
     else
         return(smaller, rem);
        

}

No comments:

Post a Comment