(Greatest Common Divisor) The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a function gcd that returns the greatest com- mon divisor of two integers.
What will be an ideal response?
```
// Finds greatest common divisor (GCD) of 2 inputs.
#include
using namespace std;
int gcd( int, int ); // function prototype
int main()
{
int a; // first number
int b; // second number
// loop for 5 pairs of inputs
for ( int j = 1; j <= 5; j++ )
{
cout << "Enter two integers: ";
cin >> a >> b;
cout << "The greatest common divisor of "
<< a << " and " << b << " is ";
// find greatest common divisor of a and b
cout << gcd( a, b ) << endl;
} // end for
} // end main
// gcd finds greatest common divisor of x and y
int gcd( int x, int y )
{
int greatest = 1; // current greatest common divisor, 1 is minimum
// loop from 2 to smaller of x and y
for ( int i = 2; i <= ( ( x < y ) ? x: y ); i++ )
{
// if current i divides both x and y
if ( x % i == 0 && y % i == 0 )
greatest = i; // update greatest common divisor
} // end for
return greatest; // return greatest common divisor found
} // end function gcd
```
You might also like to view...
Like a driver's license, digital certificates can expire or be revoked and are issued by a ________
A) security officer B) license bureau C) computer authority D) certificate authority
Federal Express (FedEx) was one of the first large companies to empower customers to serve themselves at their convenience through the use of _____.
A. a corporate extranet B. personal assistants C. automated customer service D. a mobile app