|
|
|
© 1998 by Bobby Griggs.
// Program name: GCD
// Purpose: To compute the greatest common divisor of two integers.
// Win32 console application
#pragma hdrstop
#include < iostream.h >
#include "myprocs.h"
void Input_integers(int &a,int &b);
void Calculate_gcd(int a,int b,int &g);
void Display_gcd(int a,int b,int g);
void main()
{
int num1,num2,gcd;
bool doagain = true;
while (doagain)
{
Input_integers(num1,num2);
Calculate_gcd(num1,num2,gcd);
Display_gcd(num1,num2,gcd);
doagain = Again();
}
return;
}
void Input_integers(int &a,int &b)
{
cout << "Compute GCD of two integers!" << endl << endl;
cout << "Enter first integer ... ";
cin >> a;
cout << endl;
cout << "Enter second integer ... ";
cin >> b;
cout << endl << endl;
Swap(a,b);
return;
}
void Calculate_gcd(int a,int b,int &g)
{
int i;
if (b % a == 0)
{
g = a;
return;
}
else
{
for (i = a;i >= 1;i--)
if ((b % i == 0) && (a % i == 0))
{
g = i;
return;
}
}
}
void Display_gcd(int a,int b,int g)
{
cout << "The GCD of " << a << " and " << b << " is ";
cout << g << "!" << endl << endl;
return;
}
|