Today, we’re learning and practicing an algorithmic concept called Recursion. Check out the Tutorial tab for learning materials and an instructional video!
Recursive Method for Calculating Factorial
Write a factorial function that takes a positive integer, as a parameter and prints the result of ( factorial).
A single integer, (the argument to pass to factorial).
Print a single integer denoting N!.
- factorial(3) = 3 x factorial(2)
- factorial(2) = 2 x factorial(1)
- factorial(1) = 1
Recursion is basically defining a base case and applying that base case to the function again to find results for other cases.
This example was easy because the explanation basically tells you how to code it, but once again, I struggled with reading and printing input in Hacker Rank..
Don’t forget to explicitly tell the code to print out the result of the function at the end!