Print Friendly and PDF
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 common divisor of two integers.
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 common divisor of two integers.

Example 


#include <iostream>
using namespace std;
int gcd( int, int );
int main()
{
int a, b;
for ( int j = 1; j <= 5; ++j ) {
cout << "Enter two integers: ";
cin >> a >> b;
cout << "The greatest common divisor of " << a << " and "<< b << " is " << gcd( a, b ) << "\n\n";
}
return 0;
}
int gcd( int x, int y )
{

int greatest = 1;
for ( int i = 2; i <= ( ( x < y ) ? x: y ); ++i )
if ( x % i == 0 && y % i == 0 )
greatest = i;
return greatest;
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: