DEV Community

PONVEL M
PONVEL M

Posted on

Understanding Recursion with Simple Examples (Java, Python & JavaScript)

Recursion is one of the most important concepts in programming. Once you understand it clearly, many complex problems become much easier to solve.

In this blog, we will learn recursion in a simple and practical way with examples in Python, Java, and JavaScript.

What is Recursion?

Recursion is a technique where a function calls itself to solve a problem.

Instead of using loops, recursion breaks a problem into smaller subproblems and solves them step by step.

Two Important Parts of Recursion

Every recursive function must have:

1. Base Case (Stopping Condition)

This is the condition where the function stops calling itself.

Without this → program will run forever (infinite recursion )

2. Recursive Call

The function calls itself with a smaller or simpler input.


Example 1: Print Numbers from 1 to 5

Flowchart:

Python

def display(num):
    if num > 5:
        return

    print(num)
    display(num + 1)

display(1)
Enter fullscreen mode Exit fullscreen mode

Java

class Main {
    static void display(int num) {
        if (num > 5) return;

        System.out.println(num);
        display(num + 1);
    }

    public static void main(String[] args) {
        display(1);
    }
}
Enter fullscreen mode Exit fullscreen mode

JavaScript

function display(num) {
    if (num > 5) return;

    console.log(num);
    display(num + 1);
}

display(1);
Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

Example 2: Print Odd Numbers (1 to 9)

Flowchart:

Python

def display(num):
    if num > 9:
        return

    print(num)
    display(num + 2)

display(1)
Enter fullscreen mode Exit fullscreen mode

Output:

1 3 5 7 9
Enter fullscreen mode Exit fullscreen mode

Example 3: Print Multiples of 5

Python

def display(num):
    if num > 25:
        return

    print(num)
    display(num + 5)

display(5)
Enter fullscreen mode Exit fullscreen mode

Output:

5 10 15 20 25
Enter fullscreen mode Exit fullscreen mode

Example 4: Factorial using Recursion

Factorial means:

5! = 5 × 4 × 3 × 2 × 1

Python

def fact(num, result=1):
    if num > 5:
        return result

    result *= num
    return fact(num + 1, result)

print("Factorial:", fact(1))
Enter fullscreen mode Exit fullscreen mode

Java

class Main {
    static int fact(int num, int result) {
        if (num > 5) return result;

        result *= num;
        return fact(num + 1, result);
    }

    public static void main(String[] args) {
        System.out.println("Factorial: " + fact(1, 1));
    }
}
Enter fullscreen mode Exit fullscreen mode

Example 5: Sum of First N Numbers

Flowchart:

Python

def sum_n(num, total=0):
    if num > 5:
        return total

    total += num
    return sum_n(num + 1, total)

print("Sum:", sum_n(1))
Enter fullscreen mode Exit fullscreen mode

Output:

Sum: 15
Enter fullscreen mode Exit fullscreen mode

How Recursion Works (Simple Idea)

Example:

display(1)
Enter fullscreen mode Exit fullscreen mode

It works like:

display(1)
display(2)
display(3)
display(4)
display(5)
stop
Enter fullscreen mode Exit fullscreen mode

Each function call is stored in memory until the base condition is met.


Advantages of Recursion

✔ Makes code shorter and cleaner
✔ Useful for problems like trees, graphs, factorial
✔ Easier to understand logically


Disadvantages of Recursion

Uses more memory (stack)
Can be slower than loops
Risk of infinite recursion


Final Thoughts

Recursion may feel confusing at first, but once you understand:

Base case + Recursive call = Everything

you can solve many complex problems easily.


Practice Ideas

Try solving:

  • Reverse a number using recursion
  • Fibonacci series
  • Sum of digits
  • Palindrome check

Tip: Start small, trace step-by-step, and recursion will become easy!


Happy Coding!

Top comments (0)