Solution Review: Find the Greatest Common Divisor
This lesson provides a detailed review of the solution to the challenge of the previous lesson.
We'll cover the following...
We'll cover the following...
Solution: Greatest Common Divisor
Understanding the Code
In the code above, the method gcd is recursive, since it makes a recursive call in the method body. Below is an explanation of the code above.
Driver Method
In the
main()code, we have defined three integer variables:x,yandresult.The variable
result, stores the greatest common divisor ofxandy, returned by thegcdmethod.Line 31 prints
result.
Recursive Method
The return type of this method is an integer because GCD of two integers is always an integer.
The method takes two integer input arguments
num1andnum2. ...