DEV Community

Neelakandan R
Neelakandan R

Posted on

3 2 3 3 3

Recursion in Java

Recursion in Java

In Java, Recursion is a process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function.

case Condition in Recursion

In the recursive program, the solution to the base case is provided and the solution to the bigger problem is expressed in terms of smaller problems.

int fact(int n)
{
    if (n < = 1) // base case
        return 1;
    else    
        return n*fact(n-1);    
}
Enter fullscreen mode Exit fullscreen mode

In the above example, the base case for n < = 1 is defined and the larger value of a number can be solved by converting it to a smaller one till the base case is reached.

Working of Recursion

The idea is to represent a problem in terms of one or more smaller sub-problems and add base conditions that stop the recursion. For example, we compute factorial n if we know the factorial of (n-1). The base case for factorial would be n = 0. We return 1 when n = 0

// Factorial using recursion
class GFG {

    // recursive method
    int fact(int n)
    {
        int result;

        if (n == 1)
            return 1;
        result = fact(n - 1) * n;//ex-4 mean 4*3*2*1
        return result;//ex-4 mean ans--24
    }
}

// Driver Class
class Recursion {

    // Main function
    public static void main(String[] args)
    {
        GFG f = new GFG();

        System.out.println("Factorial of 3 is "
                           + f.fact(3));
        System.out.println("Factorial of 4 is "
                           + f.fact(4));
        System.out.println("Factorial of 5 is "
                           + f.fact(5));
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120

Reference:https://www.geeksforgeeks.org/recursion-in-java/

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

Image of PulumiUP 2025

Transform Your Cloud Infrastructure

Join PulumiUP 2025 on May 6 for Expert Insights & Demos.

Register Now

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay