 Welcome to Finding Factorials with Recursion. In this learning activity, we'll discuss how to find factorials using recursion. Learning how recursion works and when it's the right choice as an algorithm for a task makes you a better computer programmer, knowing when recursion is the best solution for your computer programming problem also makes your job easier. Let's start by understanding what a factorial is. A factorial of an integer is defined as the product of all positive integers less than or equal to the original integer. So the factorial of an integer n is 1 times 2 times 3 and so on until n. Some easy examples include 4 factorial which equals 4 times 3 times 2 times 1 equaling 24. 5 factorial equals 5 times 4 times 3 times 2 times 1 equaling 120. 6 factorial equals 6 times 5 times 4 times 3 times 2 times 1 equaling 720. We can write this factorial in code pretty simply, but that's not very interesting. If you notice from our previous example, 5 factorial and 6 factorial are similar. 6 factorial can be redefined as 6 factorial equals 5 factorial times 6 and by the same logic 5 factorial can also be redefined as 5 factorial equals 4 factorial times 5 and so on. We can generalize these redefinitions by writing n factorial equals n minus 1 factorial times n. In this equation, the factorial of n is defined as the factorial of n minus 1 times n. But where does this end? Mathematically, it concludes when we get to 0. 0 factorial is equal to 1. We can now logically calculate any factorial. In code, instead of using iteration to calculate the factorial, we use recursion. In this example, instead of looping, we use recursion to get the answer. Recursion is a function that calls itself, passing in a value with each call. We can add simple logic to handle the case of a negative input. The factorial of a negative number is defined as undefined. How's that for your daily set? In order for recursion to work, we must define a base case. The base case is the state where the algorithm won't recurse. In other words, the case when the function won't call itself. Having a base case prevents the function from calling itself an infinite number of times. If there is no base case, then the recursive algorithm calls itself over and over and over again, until eventually a stack overflow exception is generated and the program crashes. All recursive calls should bring the state closer to the base case. What closer means really depends on the situation. In our example, any positive number input into the factorial function calls the factorial function again, passing in a value one less than the value that was passed to it. This happens over and over again until a zero is passed in, our base case. When this happens, the factorial function returns a one. So each recursive call to the factorial function gets one step closer to the base case, the input being zero. There are plenty of other interesting and useful things you can do with recursive algorithms. You can find every file and folder in a file system, including any subfolders. Calculate Fibonacci numbers, find the solution to a maze, and generate fractals. Using recursion to find factorials helps you understand recursion. Understanding when to use recursion to solve a programming problem saves time, improves your code, and makes you a better computer programmer. Congratulations! You have just completed finding factorials with recursion.