Source of gcd.cpp


  1: /** @file gcd.cpp
  2: Finds the greatest common divisor (gcd) of two positive integers.
  3: */

  5: #include <iostream>
  6: using namespace std;

  8: #include "utilities.h"
  9: using Scobey::Pause;
 10: using Scobey::ReadInt;
 11: using Scobey::userSaysYes;

 13: int gcd
 14: (
 15:      int a, //in
 16:      int b  //in
 17: )
 18: /**<
 19: Compute the gcd of two positive integers.
 20: @return The gcd of a and b.
 21: @param a One of the two integers whose gcd is sought.
 22: @param b One of the two integers whose gcd is sought.
 23: @pre Both a and b have been initialized with nonnegative integer
 24: values, and at least one of a or b is strictly positive.
 25: @post No other side effects.
 26: */
 27: {
 28:     if (b == 0)
 29:         return a;
 30:     else
 31:         return gcd(b, a%b);
 32: }

 34: int main()
 35: {
 36:     cout << "\nThis program finds the greatest common divisor "
 37:         "(gcd) of two positive integers. \n";
 38:     Pause();

 40:     do
 41:     {
 42:         int first, second;
 43:         ReadInt("Enter first integer: ", first);
 44:         ReadInt("Enter second integer: ", second);
 45:         cout << "gcd(" << first  << ", " << second << ") = "
 46:             << gcd(first, second) << "\n\n";
 47:     }
 48:     while (userSaysYes("Do it again?"));
 49: }