468,490 Members | 2,533 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

Fibonacci Series

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

Please Write
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 122009
Banfa
9,055 Expert Mod 8TB
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 100+
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
//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
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
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,055 Expert Mod 8TB
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
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 100+
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
#include<iostream>
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
D_C
293 100+
The way integers are ordered, are non-negative numbers, in order, followed by the reversed negative range. It goes from 0 to INT_MAX, then INT_MIN to -1. Integers are 32-bit, so in Hexadecimal:
0x00000000 = 0
0x7FFFFFF = INT_MAX = -1+(2^31)
0x80000000 = INT_MIN = -(2^31)
0xFFFFFFF = -1.

INT_MAX + 1 = INT_MIN. Since Fibonacci numbers are positive, it will overflow and become negative. Modify the for statement to a while statement, and run the loop while fib is not negative (no overflow). Then variables x1 and x2, x1 < x2, will be the two most positive Fibonacci numbers an integer can support.
Aug 6 '06 #11
Hi,
Please follow the following program,

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5.   int a =0,b=1,c,i=2,n;
  6.   clrscr();
  7.   printf("Enter N Value : ");
  8.   scanf("%d",&n);
  9.   printf("%d\t%d\t",a,b);
  10.   while(n>i)
  11.   {
  12.     c = a+b;
  13.     printf("%d\t",c);
  14.     c=b;
  15.     b=a;
  16.     i++;
  17.   }
  18.   getch();
  19. }
Take Care,
Bye... Bye...
Aug 10 '06 #12
The easiset non recursive method I see to do this is to use a stack.

stack will always have 2 elements , both will have integers, and of course pointers.

The next element will be figured by adding the 2 integers in the stack. then you will push that on the top of the stack, and pull the header off of the stack.

I hope that made sence.
Aug 11 '06 #13
hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.

Please Write
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
hello friend,
one thing is remember whenever u r doing fib series most important part is fib generate recursion that is chain is occur. so make one function is fib(). around this function write the code just like this as follows
fo=0;
f1=1
fib=fo+f1;
and display the fib.
try your self otherwise i will send u full program for fib.
thank u very much.
Sep 2 '06 #14
D_C
293 100+
Instead of a stack with two entries, just use two variables. Besides, there are already plenty of solutions posted here, and the iterative loop is considerably faster than using recursion.
Sep 2 '06 #15
i want it in c++ not c
Feb 19 '07 #16
Banfa
9,055 Expert Mod 8TB
i want it in c++ not c
And what do you think the difference should be?
Feb 19 '07 #17
hirak1984
316 100+
Do you want to get hints and try yourself?

or do you want to get a readymade code,just as you buy things from shops?

i want it in c++ not c
Feb 19 '07 #18
Expand|Select|Wrap|Line Numbers
  1. // Recursive method ...Try it.
  2.  
  3. #include <iostream.h>
  4.  
  5. static int fibVal = 0;
  6. static int f0 = 0, f1 = 1;
  7.  
  8. int fib(int n)
  9. {
  10.     if(n>0)
  11.     {
  12.         fibVal = f0 + f1;    
  13.         f0 = f1;
  14.         f1 = fibVal;
  15.         cout<<f1<<endl;
  16.         n--;
  17.         fib(n);
  18.     }
  19.     return f1;
  20. }
  21.  
  22.  
  23.  
  24. void main()
  25. {
  26.     int limit;
  27.     cout<<"Please Enter the value = ";
  28.     cin>>limit;
  29.     int answer = 0;
  30.  
  31.     if(limit == 1)
  32.     {
  33.         cout<<f0<<endl;
  34.     }
  35.     else if(limit > 1)
  36.     {
  37.         cout<<f0<<endl;
  38.         cout<<f1<<endl;
  39.         answer = fib(limit - 2);
  40.     }
  41.     else if(limit <= 0)
  42.         cout<<"Please enter the value greater than 0"<<endl;
  43.  
  44. }
Jun 4 '07 #19
lakku
4
hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.

Please Write
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
plese give me the program.
Jun 13 '07 #20
Savage
1,764 Expert 1GB
plese give me the program.
Sure,just follow this link.

Savage
Jun 13 '07 #21
Hi,

Recursion in this case might cause the stack overflow for larger series. I think the most optimized piece of code can be

-------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #include < iostream.h >
  2.  
  3. void main()
  4. {
  5.     unsigned int initial_value = 0, final_value = 1, count = 0; 
  6.  
  7.     cout << "Give the count:";
  8.     cin   >> count;
  9.  
  10.     while( count-- ) {
  11.         cout  <<  initial_value  <<  endl;
  12.         final_value += initial_value;
  13.         initial_value = final_value - initial_value;        
  14.     }
  15. }
-------------------------------------------------------------------------
It prints till count 48. Perhaps you need larger datatype insted of 'int' in case you need larger series.

Enjoy!!!
Shashikant Patil
Aug 1 '07 #22
JosAH
11,448 Expert 8TB
Hi,

Recursion in this case might cause the stack overflow for larger series. I think the most optimized piece of code can be -
Have a look at a closed form formula; it's equation (6) in this link.

kind regards,

Jos
Aug 1 '07 #23

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by student | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.