473,883 Members | 1,680 Online

Fibonacci Series

4 New Member
hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.

The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
(a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.

and than you
Aug 2 '06 #1
22 123005
Banfa
9,065 Recognized Expert Moderator Expert
This is very simply achieved with a loop, you should have a go yourself and then we will help if there are problems in your solution.

There is a formula as well which you should be able to find with a few minutes web search. However it uses the floating point calculations on an irrational number so may not be entirely suited to a computer (where floating point calculations are not exact).
Aug 3 '06 #2
D_C
293 Contributor
There is a simple recurrence relation which will help.

fibonacci(n) = fibonacci(n-1) + fibonacci(n-2). That's the recurrence relation which is recursive, but it is easily implemented as a loop.
Aug 4 '06 #3
shinelakshmanan
13 New Member
//Non recursive method
Expand|Select|Wrap|Line Numbers
1. #include<stdio.h>
2. #include<conio.h>
3.
4. void main()
5. {
6.    int initial_value=0,final_value=1,temp_value,count=10;
7. // count contains the number of elements to be generated.....
8.  for(count=1;count<=10;count++)
9.   {
10.                 temp_value=initial_value+final_value;
11.     printf("\n%d",initial_value);
12.     initial_value=final_value;
13.     final_value=temp_value;
14. }
15.
16. getch();
17. }
Aug 4 '06 #4
shinelakshmanan
13 New Member
Expand|Select|Wrap|Line Numbers
1. // Recursive Method.......
2.
3. int fibo_gen(int,int,int);
4.
5. void main()
6.
7. {
8.
9.   int initial_value=0,final_value=1,count=100;
10.   fibo_gen(initial_value,final_value,count);
11.   getch();
12.
13. }
14.
15. int fibo_gen(int initial_value,int final_value,int count)
16.
17. {
18.
19.  int temp;
20.
21. if (count>0)
22.
23.  {
24.
25.   printf("\n%d",initial_value);
26.   temp=initial_value+final_value;
27.   initial_value=final_value;
28.   final_value=temp;
29.   count=count-1;
30.   fibo_gen(a,b,c);
31.
32. }
33.
34. else
35. return(0);
36.
37. }
]
Aug 4 '06 #5
gondarala vaibhavi
1 New Member
Expand|Select|Wrap|Line Numbers
1. main()
2. {
3. int n,fib[20];
4. printf("enter the value of n");
5. scanf("%d",&n);fib[0]=0;fib[1]=1;
6. for(i=2;i<n;i++)
7. fib(i)=fib(i-1)+fib(i-2);
Aug 4 '06 #6
Banfa
9,065 Recognized Expert Moderator Expert
main()
{
int n,fib[20];
printf("enter the value of n");
scanf("%d",&n); fib[0]=0;fib[1]=1;
for(i=2;i<n;i++ )
fib(i)=fib(i-1)+fib(i-2);
You haven't output the result,
you haven't finished the program, this wont compile
if the user inputs a value >= 20 then this program writes off the end of the fib array invoking undefined behaviour.

Apart from that a very good solution :D
Aug 4 '06 #7
kookai
4 New Member
G'day

where error?

1 error(s), 0 warning(s)

Expand|Select|Wrap|Line Numbers
1. # include <iostream.h>
2. int fibonacci(int n)
3. {
4. int x1 = 0, fib;
5. int x2 = 1;
6. if(n >= 1)
7. {
8. for(int i=2;i<= n; i++)
9. {
10. fib = x1+ x2;
11. x1 = x2;
12. x2 = fib;
13. }
14.
15. return fib;
16. }
Aug 5 '06 #8
D_C
293 Contributor
You only return a value if n > 0. Afterwards, you should add "return 1; }" assuming n is non-negative. Also, usually the sequence starts with 1 and 1, not 0 and 1. You may be off by one term.
Aug 5 '06 #9
kookai
4 New Member
#include<iostre am>
using namespace std;
Expand|Select|Wrap|Line Numbers
1. int fibonacci(int n)
2. {
3.   int x1 = 0, fib;
4.   int x2 = 1;
5.   if(n >= 1)
6.   {
7.     for(int i=2;i<= n; i++)
8.     {
9.       fib = x1+ x2;
10.       x1 = x2;
11.       x2 = fib;
12.     }
13.   }
14.
15.   return fib;
16. }
17. int main(){
18.   for(int i=2;i<=10;i++) {
19.     cout<<fibonacci(i)<<endl;
20.   }
21.   return 0;
22. }
this is ok now I'm finish it
but I need this

(1) Determine the largest int Fibonacci number that can be printed on your system.
(2) Modify the program of part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified program to repeat part (b).
Aug 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.