(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...
Which command will result in records that have a Col1 value greater than 100?
A. SELECT Col1 FROM Table1 IF Col1 GT 100 B. SELECT Col1 FROM Table1 WHERE Col1 > 100 C. SELECT Col1 > 100 FROM Table1 D. IF Col1 > 100 SELECT * FROM Table1
Visual Basic Toolbox objects cannot be placed in a table on a VSTO document.
Answer the following statement true (T) or false (F)
The size of an MP3 file depends on the digital ________ rate of the song.
Fill in the blank(s) with the appropriate word(s).
A/an _________________ is the physical point where a regulated network service is provided to a user or organization.
A. entrance B. exit C. facility D. demarcation E. none of the above