473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

fibonci series and recursive function

31
i was looking for questions on while loops when i came across a problem that said write a program that displays fibonacci numbers
and the code that i wrote works just fine

Expand|Select|Wrap|Line Numbers
  1. int x1=1;
  2. int x2=1;
  3. int x3;
  4. int x;
  5. cout<<"please enter the number "<<endl;
  6. cin>>x;
  7. cout<<x1<<endl<<x2<<endl;
  8. while (x2<x)
  9. {
  10.     x3=x1+x2;
  11.     x1=x2;
  12.    x2=x3;
  13.   cout<<x1<<endl<<x2<<endl;
  14.  
  15. }
  16.  
but the second part of the question says write another program but this time using recursive funtion
can any body please explain that to me and also how can i change my program to become a recursive function
i can write this as a seprate funtion
but how to find the terminating if function and how to approach this problem using recursive I have an exam coming up please help as soon as possible
Nov 25 '06 #1
4 2359
horace1
1,510 Expert 1GB
but the second part of the question says write another program but this time using recursive funtion
can any body please explain that to me and also how can i change my program to become a recursive function
i can write this as a seprate funtion
but how to find the terminating if function and how to approach this problem using recursive I have an exam coming up please help as soon as possible
The algorithm for a recursive version of function fibonacci(int i) is
Expand|Select|Wrap|Line Numbers
  1.   if (i <= 2) return 1;
  2.   else return fibonacci(i-1) + fibonacci(i-2); 
  3.  
the recursion terminates when the function parameter i becomes less than or equal to 2
see the folowing for a discussion on binary recursion
http://www.csse.monash.edu.au/~lloyd...S/Recn/Binary/
Nov 25 '06 #2
hapa
31
The algorithm for a recursive version of function fibonacci(int i) is
Expand|Select|Wrap|Line Numbers
  1.   if (i <= 2) return 1;
  2.   else return fibonacci(i-1) + fibonacci(i-2); 
  3.  
the recursion terminates when the function parameter i becomes less than or equal to 2
see the folowing for a discussion on binary recursion
http://www.csse.monash.edu.au/~lloyd...S/Recn/Binary/
but shoudlnt the function have 2 input argument
Nov 25 '06 #3
horace1
1,510 Expert 1GB
but shoudlnt the function have 2 input argument
no the function header would be
Expand|Select|Wrap|Line Numbers
  1. int fibonacci(int i)
  2.  
it shoud return the i'th fibonacci number
Nov 25 '06 #4
hapa
31
no the function header would be
Expand|Select|Wrap|Line Numbers
  1. int fibonacci(int i)
  2.  
it shoud return the i'th fibonacci number

Thank you it works
i understand it now

done it and it works
Nov 25 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
4
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is ListType: for a in arg1: my_function(a, arg2,...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
47
by: shan | last post by:
Hi to every body, I am a begginer in C.can anybody give the complete code for fibinocci series (0 1 1 2 3 5 8 ...)an input is get from user to calculate how many numbers to be printed.I am using...
9
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
10
by: pereges | last post by:
How to to go about this ? Suppose a malloc inside a recursive function has failed and you want to set the error flag and return it to the calling function(the one which called the recursive...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
3
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.