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

  • Home
  • SEARCH
  • 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 7183285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:56:54+00:00 2026-05-28T17:56:54+00:00

Please explain this simple code: public int fibonacci(int n) { if(n == 0) return

  • 0

Please explain this simple code:

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

I’m confused with the last line especially because if n = 5 for example, then fibonacci(4) + fibonacci(3) would be called and so on but I don’t understand how this algorithm calculates the value at index 5 by this method. Please explain with a lot of detail!

  • 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-28T17:56:55+00:00Added an answer on May 28, 2026 at 5:56 pm

    In fibonacci sequence each item is the sum of the previous two. So, you wrote a recursive algorithm.

    So,

    fibonacci(5) = fibonacci(4) + fibonacci(3)
    
    fibonacci(3) = fibonacci(2) + fibonacci(1)
    
    fibonacci(4) = fibonacci(3) + fibonacci(2)
    
    fibonacci(2) = fibonacci(1) + fibonacci(0)
    

    Now you already know fibonacci(1)==1 and fibonacci(0) == 0. So, you can subsequently calculate the other values.

    Now,

    fibonacci(2) = 1+0 = 1
    fibonacci(3) = 1+1 = 2
    fibonacci(4) = 2+1 = 3
    fibonacci(5) = 3+2 = 5
    

    And from fibonacci sequence 0,1,1,2,3,5,8,13,21.... we can see that for 5th element the fibonacci sequence returns 5.

    See here for Recursion Tutorial.

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

Sidebar

Related Questions

how call TRACE in c++?please explain for example with this simple code int x
Could someone please explain to me why this simple straight forward code isnt working,
Could you please explain to me why this simple VBA code fails at the
Could you please explain why this code is not syntactically correct? private void addEditor(final
Execution of this simple code: int foo(int* a){ cout <<a=<<a; *a=1; cout <<, *a=<<*a<<endl;
Why are copy constructors unnecessary for immutable objects? Please explain this for me.
Please explain what this task is about? Create a generic linked list class that
Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Can anybody please explain how this could possibly happen? I am completely aware of
I have a feeling this XML is not valid, can someone please explain why?

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.