Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6951141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:10:53+00:00 2026-05-27T14:10:53+00:00

I am finding it very confusing to understand the concept of recursion. I am

  • 0

I am finding it very confusing to understand the concept of recursion. I am trying to trace a recursive function. Can someone please help me with that?

    public static int h(int n){
        if (n == 0)
            return 0;
        else
            return h(n-1)+1;
    }

When I write

int a = h(5);
System.out.println(a)

I dont understand how the result produced is actually coming?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:10:54+00:00Added an answer on May 27, 2026 at 2:10 pm

    First of all, if you have difficulty in understanding the concept of recursion, I think the following links will help you:

    • Recursion Programming
    • Introduction to Recursion and Memoization with Examples:
    • Recursion made simple
    • Recursion

    You may use the debugging facility on your IDE to see how it is working. You may Google for instructions on how to set beakpoints and use the debugger to step through the program.

    About the method h, It will return what you given as input(if it is a positive number or 0). Also large numbers & negative numbers will cause a StackOverflowError. To know the working you may use a print statement inside your method.

    public static int h(int n) {
        System.out.println("h(" + n + ")");
        if (n == 0) {
            System.out.println("value: 0");
            return 0;
        } else {
            System.out.println("going down");
            int temp = h(n - 1) + 1;
            System.out.println("h(" + n + ") --> " + temp);
            return temp;
        }
    }
    

    will output:

    h(5)
    going down
    h(4)
    going down
    h(3)
    going down
    h(2)
    going down
    h(1)
    going down
    h(0)
    value: 0
    h(1) --> 1
    h(2) --> 2
    h(3) --> 3
    h(4) --> 4
    h(5) --> 5
    

    The above output can be edited to show the working:

    h(5)
    |    going down
    |----h(4)
    |    |   going down
    |    |---h(3)
    |    |   |   going down
    |    |   |---h(2)
    |    |   |   |  going down
    |    |   |   |--h(1)
    |    |   |   |  |    going down
    |    |   |   |  |----h(0)
    |    |   |   |  |    |    value: 0 --> return 0;
    |    |   |   |  |    h(1) --> 1 --> h(0) + 1 = 0 + 1 = 1
    |    |   |   |  h(2) --> 2          h(1) + 1 = 1 + 1 = 2
    |    |   |   h(3) --> 3             h(2) + 2 = 1 + 1 = 3
    |    |   h(4) --> 4                 h(3) + 3 = 1 + 1 = 4
    |    h(5) --> 5                     h(4) + 4 = 1 + 1 = 5
    

    The following is the non-recursive version of the method h.

    public static int nonh(int n) {
        int result = 0;
        for (int i = n; i > 0; i--) {
            result += 1;
        }
    
        return result;
    }
    

    Hope that helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to git usage and finding it very confusing to understand
I'm finding it very convenient to pass configuration and other data that is read
Finding windows FastCGI help is proving very difficult. Any help would be greatly apprciated.
This may be a very basic question. But I am finding it confusing with
I am finding it very hard to understand the way the inverse of the
I'm trying to generate a ms-word document with Yii. I'm finding very difficult to
A question I haven't seen answered that I'm finding very interesting. All the other
I am newbie and finding it very hard to grasp the syntax of Class
Currently I am designing a website and I am finding it VERY difficult to
Today is my first day using ASP.NET MVC, and I'm finding it very intriguing.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.