Question: Using c++, create a function called gcd that will take two parameters of a and b and return the greatest common denominator. Have the main
Using c++, create a function called gcd that will take two parameters of a and b and return the greatest common denominator.
Have the main program input the numbers, check to make sure the first number is not zero and both numbers are not negative. If they are incorrect, print an error message. Otherwise call the gcd function and print the answer.
Requirement: Have the gcd function be recursive.
The algorithm is based on below facts.
If we subtract smaller number from larger (we reduce larger number), GCD doesnt change. So if we keep subtracting repeatedly the larger of two, we end up with GCD.
Now instead of subtraction, if we divide smaller number, the algorithm stops when we find remainder 0.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
