Write a C++ code for calculating the GCD (Greatest Common Divisor) between two p
ID: 3549169 • Letter: W
Question
Write a C++ code for calculating the GCD (Greatest Common Divisor) between two positive integer numbers. You should use Euclid's original subtraction based GCD algorithm ( not using modulo operator). You should write a function following the prototype below: int GCD(int a, int b) Write a C++ code for calculating the GCD (Greatest Common Divisor) between two positive integer numbers. You should use Euclid's original subtraction based GCD algorithm ( not using modulo operator). You should write a function following the prototype below: int GCD(int a, int b)Explanation / Answer
using namespace std;
int EuclidGcd(int, int);
int main()
{
int a = 84;
int b = 49;
cout << "Greatest common divisor of "
<< a << " and " << b << " is "
<< EuclidGcd(a, b) << endl;
cin.get();
return 0;
}
//---------------------------------------------------------
int EuclidGcd(int a, int b)
{
if (a <= 0 || b <= 0)
return 0;
if (a > b)
if (a % b)
return EuclidGcd(b, a % b);
else
return b;
else
if (b % a)
return EuclidGcd(a, b % a);
else
return a;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.