Fibonacci with recursion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tired
    New Member
    • Feb 2009
    • 13

    Fibonacci with recursion

    hey..
    can u help me wid this..
    #include<stdio. h>
    #include<conio. h>
    void main()
    {
    int fib(int);
    int n,f=1,ans;
    clrscr();
    while(f==1)
    {
    printf("\n***Ge nerating A Fibonacci Series***");
    printf("\nEnter Number Of Terms Required: ");
    scanf("%d",&n);
    if(n<=1)
    {
    printf("\nInval id Choice..Number Of Terms Should Be more Than 1");
    printf("\nDo You Want To Try Again...\n1.Yes \n2.No");
    scanf("%d",&f);
    }
    else
    {
    printf("\nSerie s: %d",fib(n));
    f=2;
    }
    }
    getch();
    }
    int fib(int a)
    {
    if((a==0)||(a== 1))
    {
    return(1);
    }
    else
    {
    return(fib(a-1)+fib(a-2));
    }
    }
    i wat to print d numbers as well...eg if n =5
    1 1 2 3 5
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Originally posted by Tired
    hey..
    can u help me wid this..

    i wat to
    You what to what?

    void fibfib(int n)
    {
    int q;
    if(q=n-1)fibfib(q);pri ntf("%d ",fib(q));
    }

    Comment

    • Tired
      New Member
      • Feb 2009
      • 13

      #3
      wat=*want...
      umm i dint understand the func u hv typed..can u help me with understanding it???it wud b great if u can..

      thnx..

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Tired
        it wud b great if u can..

        thnx..
        Please spell your words properly; this is an international forum not your local text speak community.

        kind regards,

        Jos

        Comment

        • Tired
          New Member
          • Feb 2009
          • 13

          #5
          I am sorry...but can u please help me understand this...
          void fibfib(int n)
          {
          int q;
          if(q=n-1)fibfib(q);pri ntf("%d ",fib(q));
          }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Tired
            I am sorry...but can u please help me understand this...
            void fibfib(int n)
            {
            int q;
            if(q=n-1)fibfib(q);pri ntf("%d ",fib(q));
            }
            Note that fib() is your function; the function fibfib() is just defined for printing the return values of your function. Try it by hand for small values of n. Also note that the expression q=n-1 is non-zero when n != 1

            kind regards,

            Jos

            Comment

            • Tired
              New Member
              • Feb 2009
              • 13

              #7
              But its not printing all the values(or the entire series). Its just printing the final answer.Where should i call this function?in the main block before i am printing my answer?
              And most of the times its not printing any thing.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Tired
                But its not printing all the values(or the entire series). Its just printing the final answer.Where should i call this function?in the main block before i am printing my answer?
                And most of the times its not printing any thing.
                How strange; to be sure I ran it as well and it runs fine, i.e. it prints out the first n values of the standard Fibonacci series. Can you show us some code?

                kind regards,

                Jos

                Comment

                • Tired
                  New Member
                  • Feb 2009
                  • 13

                  #9
                  #include<stdio. h>
                  #include<conio. h>
                  void main()
                  {
                  int fib(int);
                  void fibfib(int);
                  int n,f=1,ans;
                  clrscr();
                  while(f==1)
                  {
                  printf("\n***Ge nerating A Fibonacci Series***");
                  printf("\nEnter Number Of Terms Required: ");
                  scanf("%d",&n);
                  if(n<=1)
                  {
                  printf("\nInval id Choice..Number Of Terms Should Be more Than 1");
                  printf("\nDo You Want To Try Again...\n1.Yes \n2.No");
                  scanf("%d",&f);
                  }
                  else
                  {
                  fibfib(n);
                  printf("\nAns i.e. term no. %d = %d",n,fib(n)) ;
                  printf("\nDo You Want To Continue:\n1.Ye s\n2.No\n");
                  scanf("%d",&f);
                  }
                  }
                  }
                  int fib(int a)
                  {
                  if((a==0)||(a== 1))
                  return(1);
                  else
                  return(fib(a-1)+fib(a-2));
                  }
                  void fibfib(int a)
                  {
                  int q;
                  if(q=a-1)
                  fibfib(q);
                  else
                  printf("%d ",fib(q));
                  }

                  this is the code i tried.....

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by Tired
                    Code:
                    void fibfib(int a)
                    {
                          int q;
                          if(q=a-1)
                               fibfib(q);
                          else
                                printf("%d ",fib(q));
                    }
                    this is the code i tried.....
                    I don't see an 'else' in the original code ...

                    kind regards,

                    Jos

                    Comment

                    • Tired
                      New Member
                      • Feb 2009
                      • 13

                      #11
                      it doesnt work without else also!!!i tried..so then is that last print statement not a part of the for loop??and is the way i am calling the function right??

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by Tired
                        it doesnt work without else also!!!i tried..so then is that last print statement not a part of the for loop??and is the way i am calling the function right??
                        Don't shout; there is no need to start a "yes it does, not it doesn't" discussion. You have to show or prove that it doesn't work; at least show the code you used. I also ran it and it worked for me (both in C and C++, my Java version also ran correctly).

                        kind regards,

                        Jos

                        Comment

                        • khaichiew85
                          New Member
                          • Feb 2009
                          • 10

                          #13
                          i have tried ur code..no problem with that.
                          But i would like to advise u that without detecting if the user want to continue by pressing 1 or Y or 2 or N.Use isdigit to differentiate them else if when alphabet is pressed the loop will keep on going

                          Comment

                          • Tired
                            New Member
                            • Feb 2009
                            • 13

                            #14
                            hey khaichiew85,
                            so u mean that the "fibfib" func is displaying the series for u?can u help with the calling statement and evrything??

                            regards
                            Amy

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by Tired
                              hey khaichiew85,
                              so u mean that the "fibfib" func is displaying the series for u?can u help with the calling statement and evrything??
                              There is no need for help: I simply copied/pasted that fibfib function, crafted a loop around it and voila. Why don't you show your code? We are not going to spoonfeed you complete programs.

                              kind regards,

                              Jos (moderator)

                              Comment

                              Working...