How to write a Fibonacci series in recursion

10 pts.
Tags:
C
Fibonacci series
How do I write a Fibonacci series using recursion?

Software/Hardware used:
c++

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • TomLiotta
    Here's an example of using recursion to print the Fibonacci numbers from F2 through F17 (where F0 is o, and F1 is 1). The main proc sets up the conditions; then the GenFibo proc recursively calls itself up to the initial limit.
    .
    The main proc:
    pgm
                                                    /* */
       dcl   &limit     *int            value( 16 )
       dcl   &seed1     *int            value( 0 )
       dcl   &seed2     *int            value( 1 )
                                                    /* */
       ovrdbf      STDOUT  +
                     tofile( QPRINT )  +
                     ovrscope( *JOB ) +
                     share( *YES )
                                                    /* */
       callprc  GenFibo  ( &limit &seed1 &seed2 )
                                                    /* */
       dltovr      STDOUT  lvl( *JOB )
                                                    /* */
       return
                                                    /* */
    endpgm
    The GenFibo proc is:
    pgm    ( +
             &limit       +
             &seed1       +
             &seed2       +
           )
                                                    /* */
       dcl   &limit     *int
       dcl   &seed1     *int
       dcl   &seed2     *int
       dcl   &seed3     *int
                                                    /* */
       dcl   &cSeed       *char   10
                                                    /* */
       dcl   &prtTxt      *char   32
                                                    /* */
       dcl   &NL          *char    1     value( x'15' )
       dcl   &x00         *char    1     value( x'00' )
                                                    /* */
       dcl   &rc          *int
                                                    /* */
       chgvar   &seed3   ( &seed1 + &seed2 )
       chgvar   &limit   ( &limit - 1 )
                                                    /* */
       chgvar     &cSeed              ( &seed3 )
       chgvar     &prtTxt             ( +
                                        'Next '      *cat  +
                                        &cSeed       *tcat +
                                        &NL          *cat  +
                                        &x00               +
                                      )
                                                    /* */
       callprc        'printf'            ( +
                                            &prtTxt        +
                                            *omit          +
                                          ) +
                                    rtnval( &rc )
                                                    /* */
       if ( &limit *gt 0 )  +
          callprc  GenFibo  ( &limit &seed2 &seed3 )
                                                    /* */
       return
                                                    /* */
    endpgm
    (The comment marks on every blank are needed because of what this editor does to blank lines in code-blocks.)
    .
    It's not C++, but for the most part, recursion is recursion is recursion...
    .
    If it can be done in ILE CL, C++ should be easy. What problem are you having? Show us the code you have so we can comment on it. If we can't see it, we can't say anything useful.
    .
    Tom
    125,585 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following