Any questions before we start?
Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself.
we can define the operation "Go to Home" as:
Base Case
1. If you are at home, stop moving.
Recursive Case
2. Take one step toward home.
3. "Go To Home".
Text
//A Simple Recursive Function
static void goHome(x,Home){
if(x == Home){
print("Reached Home")
return;
}
x = x + 1
goHome(x,Home)
}
//Main
int x = 1
int Home = 10
goHome(x,Home);
Recursion = Principal of Mathematical Induction
1. Figure of the Smallest Case
2. Always Assume the Subproblem Can be Solved
3. Solve the current problem assuming subproblem solution exists
Input
5
Output
120
Given N, print numbers from 1 to N without using a Loop (Use Recursion)
Input
5
Output
1, 2, 3, 4, 5
Given N, print numbers from 1 to N without using a Loop (Use Recursion)
Input
5
Output
5, 4, 3, 2, 1
Given N, print the sum of digits of that number using recursion.
Input
512
Output
8