473,320 Members | 2,071 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,320 software developers and data experts.

need urgent help in coding fibonacci series in c++

plz help to find why the program showing fibonacci(non-recursively) is running infinitely
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. unsigned int fibo(int a,int b)
  5. {
  6.     unsigned int i,sum=0,n;
  7.  
  8.     for(i=1;i<=n;i++)
  9.     {
  10.       sum=a+b;
  11.       cout<<","<<sum;
  12.       a=b;
  13.       b=sum;
  14.       }
  15.       return sum;
  16. }
  17. int main()
  18. {
  19.     unsigned int x=0,y=1,z;
  20.     cout<<"enter a number to display series"<<endl;
  21.     cin>>z;
  22.     cout<<x;
  23.     cout<<","<<y;
  24.     fibo(x,y);
  25.     getch();
  26. }

plz check the error in fibonacci program done recursively
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. unsigned int fib(int n)
  5. {
  6.         unsigned int sum=0;
  7.  
  8.             if(n==0) 
  9.              {
  10.                sum=0;
  11.                 }
  12.               else if(n==1)
  13.               { 
  14.                   sum=1;
  15.                   }
  16.                   else
  17.                   { 
  18.                    sum=fib(n-1)+fib(n-2);
  19.                     cout<<","<<sum;
  20.                  }
  21.                return sum;
  22. }
  23. int main()
  24. {
  25.     unsigned int x;
  26.     cout<<"enter a number to display series"<<endl;
  27.     cin>>x;
  28.      fib(x);
  29.  
  30.  
  31.     getch();
  32. }
Dec 3 '06 #1
6 5493
horace1
1,510 Expert 1GB
in the first programyou do not initialise n which you then use to control the for() loop
Expand|Select|Wrap|Line Numbers
  1.     unsigned int i,sum=0,n;
  2.  
your recursive fibonacci program is Ok, you just were not printing the result, e.g.
Expand|Select|Wrap|Line Numbers
  1. cout << "\nresult " << fib(x);
  2.  
Dec 3 '06 #2
thanxs for the reply but the non-recursive function is not showing the full output even on initializing n.eg-n=6
output=0,1
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. unsigned int fibo(int a,int b)
  5. {
  6.     unsigned int i,sum=0,n=1;
  7.  
  8.     for(i=1;i<=n;i++)
  9.     {
  10.       sum=a+b;
  11.       cout<<","<<sum;
  12.       a=b;
  13.       b=sum;
  14.       }
  15.       return sum;
  16. }
  17. int main()
  18. {
  19.     unsigned int x=0,y=1,z=1;
  20.     cout<<"enter a number to display series"<<endl;
  21.     cin>>z;
  22.     cout<<x;
  23.     cout<<","<<y;
  24.     fibo(x,y);
  25.     getch();
  26. }
the recursive function is also not showing correct output
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. unsigned int fib(int n)
  5. {
  6.         unsigned int sum=0;
  7.  
  8.             if(n==0) 
  9.              {
  10.                sum=0;
  11.                 }
  12.               else if(n==1)
  13.               { 
  14.                   sum=1;
  15.                   }
  16.                   else
  17.                   { 
  18.                    sum=fib(n-1)+fib(n-2);
  19.                     cout<<","<<sum;
  20.                  }
  21.                return sum;
  22. }
  23. int main()
  24. {
  25.     unsigned int x;
  26.     cout<<"enter a number to display series"<<endl;
  27.     cin>>x;
  28.  
  29.      cout<<"result"<<fib(x)<<endl;
  30.  
  31.  
  32.     getch();
  33. }
eg-n=6
output=1,2,1,3,1,2,5,1,2,1,3,8result=8
plz reply soon for both programs
Dec 3 '06 #3
horace1
1,510 Expert 1GB
the recursive function is also not showing correct output
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. unsigned int fib(int n)
  5. {
  6.         unsigned int sum=0;
  7.  
  8.             if(n==0) 
  9.              {
  10.                sum=0;
  11.                 }
  12.               else if(n==1)
  13.               { 
  14.                   sum=1;
  15.                   }
  16.                   else
  17.                   { 
  18.                    sum=fib(n-1)+fib(n-2);
  19.                     cout<<","<<sum;
  20.                  }
  21.                return sum;
  22. }
  23. int main()
  24. {
  25.     unsigned int x;
  26.     cout<<"enter a number to display series"<<endl;
  27.     cin>>x;
  28.  
  29.      cout<<"result"<<fib(x)<<endl;
  30.  
  31.  
  32.     getch();
  33. }
eg-n=6
output=1,2,1,3,1,2,5,1,2,1,3,8result=8
plz reply soon for both programs
the 6th fibonacci should be 8?, counting from 0 the series is 0, 1, 1, 2, 3, 5, 8, 13
Dec 3 '06 #4
i'm not getting correct output till now plz inform me of the error in the program .both recursive and non-recursive are giving incorrect output.
plzzzzzzzzzz help soon
Dec 3 '06 #5
i'm not getting correct output till now plz inform me of the error in the program .both recursive and non-recursive are giving incorrect output.
plzzzzzzzzzz help soon
hi fibonacci recuressive program in C

<Code removed by MODERATOR - Please read the Posting Guidelines>
Feb 21 '08 #6
Ganon11
3,652 Expert 2GB
@govindasamyr:

Please take a read-through of our Posting Guidelines, found under the Help link at the top of our web page. Specifically, we do not simply post fully-coded solutions for the question asked - we are always here to help, not finish as quickly as possible. The old "Teach a man to fish" adage comes to mind.

@deepner:
Maybe you can use the output you are getting to determine what is not functioning. Finding out what your code is actually doing is a critical skill in debugging your own code, so I strongly suggest you try and figure it out on your own as much as possible.
Feb 21 '08 #7

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

Similar topics

28
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers,...
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...
11
by: MARQUITOS51 | last post by:
Hey guys this is the fibonacci series. But Im having this huge problem. First of all I want to do the simplest code possible so then I can use user defined functions and arrays. But this one didnt...
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...
3
by: greek | last post by:
Hi! I hav to generate fibonaaci series using recursion: 0,1,1,2,3,5,8,18,21... whr fibonacci(0)=0 fibonacci(1)=1 fibonacci(n)=fibonacci(n-1)+fibonacci(n-2) ive witten the code but having 2...
3
by: veeru | last post by:
Hi All, Can anyone tell about how to create a FIBONACCI series in VB.Net and C# Thanks in Advance, Veeru
2
by: guneet bhatia | last post by:
please help with fibonacci series using recursion..
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.