473,883 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
+ 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 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.

Similar topics

4
4878
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 not? Or, another way to look at this is that: Would anyone of you think it is worth a while to find out if there is any location on earth with the set of longitude and latitude numbers that coincides with the Fibonacci series? As I see it, if...
12
2713
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 newsgroup there is an ongoing discussion about the famous fibonacci sequences, and a fine program written by a poster got my attention so just for fun I wrote one myself and put it on the web just about two days ago. It can be found on the site ...
4
4885
by: student | last post by:
Hi guys, please tell me how to write the iterative version of fibonacci series using a stack. Thanks
8
10977
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 example if the user gives 7 terms of the series to be displayed, then the display of the fibonacci series is 0, 1,1, 2, 3, 5, 8. But the natural numbers not involved are 4, 6 and 7. That's what my teacher wants. But I am struggling to write a...
2
4393
by: guneet bhatia | last post by:
please help with fibonacci series using recursion..
2
3217
by: rajeshsuri | last post by:
hi this is rajesh please send program for fibonacci series using pointers or functions
10
4023
by: omran11 | last post by:
how to generate fibonacci series (lesser than 100)
0
9934
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11137
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10736
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10840
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10409
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9570
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4214
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.