LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2016, 08:53 PM   #1
bikal
Member
 
Registered: Apr 2016
Distribution: Kali linux
Posts: 43

Rep: Reputation: Disabled
Problem in understanding fibonacci series using recursion in "C"


Can anyone tell me what is going on in this program that makes it display the fibonacci series.
Code:
#include <stdio.h>
int fibo(int a);
int main (void)
{
    int i;
    for(i=1;i<=5;i++)
    {
        printf("%d ",fibo(i));
    }
}
int fibo(int a)
{
    if(a==0)
        return 0;
    else if(a==1)
        return 1;
    else
        return (fibo(a-1)+fibo(a-2));
}
 
Old 06-15-2016, 11:53 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Do you not understand what the fibonacci sequence is or you do not understand recursive functions?? Either can be found with a simple search.

As far as the code goes, there are no complicated commands or functions, so if you really do not follow it you may need to take a course in C programming
 
2 members found this post helpful.
Old 06-16-2016, 07:00 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,632
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
Notice, in particular, how the fibo function calls itself.

A common, common-sense description of this number series is "naturally recursive," and the fibo function is a direct implementation of that definition. The definition of the first two numbers is fixed. The definition of any and every subsequent number in the series is defined as the sum of the numbers that preceded it. (The series can be calculated recursively, or as a simple loop.)

"Towers of Hanoi" is another commonly-used textbook demonstration of recursion.

Last edited by sundialsvcs; 06-16-2016 at 07:02 AM.
 
Old 06-16-2016, 07:57 PM   #4
bikal
Member
 
Registered: Apr 2016
Distribution: Kali linux
Posts: 43

Original Poster
Rep: Reputation: Disabled
Sorry but I think that wasn't a helpful reply. The problem is I cannot understand the recursive nature of this program
 
Old 06-16-2016, 08:20 PM   #5
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

Rep: Reputation: Disabled
Quote:
Originally Posted by bikal View Post
Sorry but I think that wasn't a helpful reply. The problem is I cannot understand the recursive nature of this program
I think the reply was pretty helpful. Assuming you know that each digit in the Fibonacci sequence is the sum of the two previous digits, it should be pretty clear how it works.

The "fibo" function simply returns the number corresponding to a given position in the sequence by finding the sum of the two previous digits, and it does so by means of recursion, except for positions 0 and 1 which are treated as special cases:
  • fibo(0) and fibo(1) will return 0 or 1 respectively
  • fibo(2) will return the sum of fibo(1) and fibo(0)
  • fibo(3) will return the sum of fibo(2) and fibo(1)
...and so on.

This is a classic example of recursion, and you can do this with pretty much any problem that can be split into two parts, where one part ends up being just a smaller version of the original problem. For instance, a digit sum can be described as the value of the first digit plus the digit sum of the remaining digits.
 
Old 06-16-2016, 11:27 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> The problem is I cannot understand the recursive nature of this program.

That's sad. And what could be done to remedy this situation?
 
Old 06-17-2016, 09:28 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,632
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
What's there to "understand?" Recursion is defined as: "a function calls itself."
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Significance of "WARNING: recursion requested but not available" from ISP? catkin Linux - General 2 07-15-2014 03:59 AM
Problem understanding IPtables "-d" param resetreset Linux - Networking 4 04-16-2012 03:46 AM
"du" command without recursion dipuasks Linux - General 4 05-19-2008 07:48 AM
"bad state" problem for hp nx 7400 series deggial Slackware 1 02-01-2007 12:07 AM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration