Recursion

Any questions before we start?

Recursion Basics


Recursion
Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself.
Example
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".

Code
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
Factorial of Number

Input
5
Output
120
Call Stack! 👨💻
Time & Space Utilisation 👨💻
Increasing Numbers

Given N, print numbers from 1 to N without using a Loop (Use Recursion)
Input
5
Output
1, 2, 3, 4, 5
Decreasing Numbers

Given N, print numbers from 1 to N without using a Loop (Use Recursion)
Input
5
Output
5, 4, 3, 2, 1
Sum the Digits

Given N, print the sum of digits of that number using recursion.
Input
512
Output
8
[Java 07] Recursion
By Prateek Narang
[Java 07] Recursion
- 11