DEV Community

Swapnil Gupta
Swapnil Gupta

Posted on

3 2

Recursion 101

Recursion is always Notorious among programmers, also confusing for the new programmers and beginners. Recursion simply refers to the program or function which calls itself.
Today, I will simplify some recursion problems and concepts inherited.

What is Recursion?
when to use Recursion?
Recursion problems and key takeaways

When a function calls itself until a specified condition is met.

void foo(int i){
print(1);
foo();
}
main(){
foo(i);
}

Enter fullscreen mode Exit fullscreen mode

the above function keeps calling itself and there would be no stopping and printing 1, it is infinite recursion. This will lead to stack overflow, filling the stack till all the memory space.

To avoid that make sure to write base condition.

cnt=0
foo(){
if(cnt==4)
return;
print(cnt)
cnt++
f()
} 

main(){
f();
}
Enter fullscreen mode Exit fullscreen mode

Section 1: Easy Problems:
Problem 1: Printing name N times using recursion in Java

 public class Main
{

    static void printName(int n){
        while(n<1) {
            return;
        }
        n--;
          System.out.println("swapnil");
          printName(n);


    }


    public static void main(String[] args) {
    java.util.Scanner sc= new java.util.Scanner(System.in);
     int n= sc.nextInt();
     int i=1;
        printName(n);
    }
}

Enter fullscreen mode Exit fullscreen mode

Problem 2: Print linearly from N to 1 i.e.
reverse fashion

public class Main
{

    static void printNum(int i,int n){
        if(i<1){
            return;
        }
        else{
          System.out.println(i);
          printNum(i-1,n);
        }

    }


    public static void main(String[] args) {
    java.util.Scanner sc= new java.util.Scanner(System.in);
     int n= sc.nextInt();
     int i=n;
        printNum(i,n);
    }
}

Enter fullscreen mode Exit fullscreen mode

problem 3: printing 1 to n using backtracking
p.s.: you are not allowed to use f(i+1,N)
you need to think in that way the base case is true, after the function is executed first i.e. function keeps calling function then operation, unlike in pervious first operation is done then function is called

public class Main
{

    static void printNum(int i,int n){
        if(i<1){
            return;
        }
        else{
          printNum(i-1,n);
         System.out.println(i);
        }

    }


    public static void main(String[] args) {
    java.util.Scanner sc= new java.util.Scanner(System.in);
     int n= sc.nextInt();
     int i=n;
        printNum(i,n);
    }
}


Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay