473,472 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.

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 122918
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<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 Contributor
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
suresh_punniyakkodi
20 New Member
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
DrXavier
3 New Member
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
sach06jan
2 New Member
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 Contributor
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
dude dash
1 New Member
i want it in c++ not c
Feb 19 '07 #16
Banfa
9,065 Recognized Expert Moderator Expert
i want it in c++ not c
And what do you think the difference should be?
Feb 19 '07 #17
hirak1984
316 Contributor
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
vikasverma
1 New Member
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 New Member
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 Recognized Expert Top Contributor
plese give me the program.
Sure,just follow this link.

Savage
Jun 13 '07 #21
shashikantnpatil
1 New Member
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 Recognized Expert MVP
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: YS Sze | last post by:
If you know the exact longitude and latitude for a specific location, would anyone think it'd make any sense to find out if this set of location numbers is really part of the Fibonacci series or...
12
by: CII | last post by:
Hi everybody. I've been reading posts a year old about the fibonacci series right on this newsgroup, and while it's not directly games related, I'll share my own notes as well. On another...
4
by: student | last post by:
Hi guys, please tell me how to write the iterative version of fibonacci series using a stack. Thanks
8
by: srinpraveen | last post by:
I know to write a program to print the fibonacci series. But the problem is my teacher has asked us to write a program to print the natural numbers that are not involved in the fibonacci series. For...
2
by: guneet bhatia | last post by:
please help with fibonacci series using recursion..
2
by: rajeshsuri | last post by:
hi this is rajesh please send program for fibonacci series using pointers or functions
10
by: omran11 | last post by:
how to generate fibonacci series (lesser than 100)
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.