Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,262 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,495 people online right now. Registration is fast and FREE... Join Now!




help correcting wrong output

 
Reply to this topicStart new topic

help correcting wrong output, coding of fibonacci series

deepner
3 Dec, 2006 - 06:24 AM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 2


My Contributions
plz check my coding to create fibonacci series i.e1,1,2,3,5,8,..
my both programs are showing incorrect output
CODE
#include<iostream>
#include<conio.h>
using namespace std;
unsigned int fibo(int a,int b)
{
    unsigned int i,sum=0,n=1;
    
    for(i=1;i<=n;i++)
    {
      sum=a+b;
      cout<<","<<sum;
      a=b;
      b=sum;
      }
      return sum;
}
int main()
{
    unsigned int x=0,y=1,z=1;
    cout<<"enter a number to display series"<<endl;
    cin>>z;
    cout<<x;
    cout<<","<<y;
    fibo(x,y);
    getch();
}


through recursive function
CODE
#include<iostream>
#include<conio.h>
using namespace std;
unsigned int fib(int n)
{
        unsigned int sum=0;
      
            if(n==0)
             {
               sum=0;
                }
              else if(n==1)
              {
                  sum=1;
                  }
                  else
                  {
                   sum=fib(n-1)+fib(n-2);
                    cout<<","<<sum;
                 }
               return sum;
}
int main()
{
    unsigned int x;
    cout<<"enter a number to display series"<<endl;
    cin>>x;
  
     cout<<"result"<<fib(x)<<endl;
  
    
    getch();
}


plz reply soon


User is offlineProfile CardPM
+Quote Post

Xing
RE: Help Correcting Wrong Output
3 Dec, 2006 - 07:37 AM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
See the modified first code
CODE

#include<iostream>
using namespace std;

unsigned int fibo(int a,int b, int n)
{
    int i,sum=0;

    for(i=1;i<=n;i++)
    {
      sum=a+b;
      cout<<","<<sum;
      a=b;
      b=sum;
      }
      return sum;
}
int main()
{
    unsigned int x=0,y=1,z=0;
    cout<<"enter a number to elements you want in series"<<endl;
    cin>>z;
    cout<<x;
    cout<<","<<y;
    fibo(x,y,z-2);

}

User is offlineProfile CardPM
+Quote Post

deepner
RE: Help Correcting Wrong Output
3 Dec, 2006 - 08:01 AM
Post #3

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 2


My Contributions
thanxs for the help but i want to do it with two variables only and plz find out the error of the recursive function also
plz reply soon cool.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:14PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month