CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Threaded View

  1. #1
    Join Date
    Feb 2014
    Posts
    11

    fibonacci number

    Hi, the next code is right. I mean it works. It gives the sequence of fibonaci numbers ...


    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int fibo(int pos)
    {
     
      if(!pos)return 0;
      if(pos==1)return 1;
    
      int pos2=--pos;
      int pos1=--pos;
    
      int r1=fibo(pos1);
      int r2=fibo(pos2);
    
      int r=r1+r2;
    
      return r;
    }
    
    int main()
    {
    
      int number;
      cin>>number;
      
      for(int p=0;p<number;++p)
        {
          int r=fibo(p);
          cout<<p<<"  "<<r<<endl;
        }
    
      cout<<endl;
    
      return  0;
    }
    but....
    when I chage the lines:
    Code:
    int r1=fibo(pos1);
      int r2=fibo(pos2);
    
      int r=r1+r2;
    for the lines:

    Code:
    int r=fibo(pos1)+fibo(pos2);
    It doen't work right. Apararently it should work the same.
    Can anyone give an explanation to this...?????

    Thanks
    Last edited by Jose M; February 16th, 2014 at 06:01 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured