## 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++

Thanks. We'll let you know when a new response is added.

## Discuss This Question: 1 Reply

Thanks. We'll let you know when a new response is added.
• 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