1 of 13

What is recursion?

2 of 13

Recursion

  • Recursion is when a method calls itself until some terminating condition is met, known as the base case.
  • Recursion can be used in situations where the next result of an operation depends on the current result.
  • Some good examples including the Fibonacci sequence or a factorial function
  • Each step until the base case is reached is called a recursive step.
  • All recursive functions can be written iteratively (without calling itself)

3 of 13

4 of 13

What if we don’t

have a base case?

5 of 13

Stack overflow

occurs. WHY?

6 of 13

Recursion Examples

7 of 13

Write a Java program that sums the numbers from 1 to 10:

Iterative method

?

Recursive method

?

8 of 13

Write a Java program that sums the numbers from 1 to 10:

Iterative method

Recursive method

?

public class SumNumbers {

public static void main(String[] args) {

int sum = 0;

for (int i = 1; i <= 10; i++) {

sum += i; // Add each number to sum

}

System.out.println("The sum of numbers from 1 to 10 is: " + sum);

}

}

9 of 13

Write a Java program that sums the numbers from 1 to 10:

Iterative method

public class SumNumbers {

public static void main(String[] args) {

int sum = 0;

for (int i = 1; i <= 10; i++) {

sum += i; // Add each number to sum

}

System.out.println("The sum of numbers from 1 to 10 is: " + sum);

}

}

Recursive method

int n = 10; // Define the upper limit

int result = sum(n);

System.out.println("The sum of numbers from 1 to " + n + " is: " + result);

}

// Recursive method to calculate the sum

public static int sum(int n) {

if (n <= 0) {

return 0; // Base case: if n is 0 or less, the sum is 0

} else {

return n + sum(n - 1); // Recursive case: sum of n and sum of numbers from 1 to n-1

}

}

}

  • 13

10 of 13

Write a Java program that calculates the factorial of 5.

Iterative method

?

Recursive method

?

11 of 13

Write a Java program that calculates the factorial of 5.

Iterative method

Recursive method

public class FactorialCalculation {

public static void main(String[] args) {

int number = 5; // Number to calculate the factorial of

int factorial = 1;

for (int i = 1; i <= number; i++) {

factorial *= i; }

System.out.println("The factorial of " + number + " is: " + factorial);

}

}

?

int number = 5; // Number to calculate the factorial of

int factorial = 1;

for (int i = 1; i <= number; i++) {

factorial *= i; }

System.out.println("The factorial of " + number + " is: " + factorial);

}

12 of 13

Write a Java program that calculates the factorial of 5.

Iterative method

Recursive method

int number = 5; // Number to calculate the factorial of

int factorial = 1;

for (int i = 1; i <= number; i++) {

factorial *= i; }

System.out.println("The factorial of " + number + " is: " + factorial);

}

int number = 5; // Number to calculate the factorial of

int result = factorial(number);

System.out.println("The factorial of " + number + " is: " + result);

}

public static int factorial(int n) {

if (n == 0) {

return 1; // Base case: factorial of 0 is 1

} else {

return n * factorial(n - 1); // Recursive case: n * factorial of n-1

}

}

13 of 13

Course website

  • Please review the resources and watch the videos provided in the course website.