Learn the process of recursion in this tutorial, using the concept for a main method that is used to test three functions: print a list in reverse, find the factorial of a number, and find the gcd of two numbers.
- [Instructor] Now we're ready to take a look at some…programming examples of recursion.…For this movie, I have already created a template…in my Exercise Folder under 01_03…that can be used as a starting point.…The main method has been set up to test three functions.…The first one will print a list in reverse order.…Followed by, finding the factorial of any natural number.…And finally, finding the greatest common divisor…of any two numbers.…Let's start with the task of printing a list…in reverse order.…
We know we need to figure out what our base case is.…So we'll set it up so that we print out the list…in reverse order, and then when there's no items left…we'll print out the first item.…So the idea here, in the recursive function of number two…is to print the list, except for the first item…in reverse order.…And what we do is we constantly shrink our list down…so that we pull the first item out each time…until the list is empty…and then we go ahead and print them…starting with the last one, going to the first one.…
Programmers involved in mathematical computations, such as mathematical induction, are probably the biggest users of recursion. You probably know some of the most common recursive problems; finding the factorial of a number and the Fibonacci series are both examples of recursive processes. In this course, staff instructor and Java expert Peggy Fisher explores programming solutions involving both of these problems. She reviews the concept of recursion, discusses approaches to solving problems using recursion, and examines some recursive examples.
- Explain how many steps it will take to move four disks in Towers of Hanoi.
- Describe the process of printing a LinkedList
- Review how to write a power function.
- Define recursion.
- Identify recursive examples.