ABAP/4 Example Code: Fibonacci


DATA: output TYPE i.
PARAMETERS: p_num TYPE i.
START-OF-SELECTION.
  PERFORM fibonacci USING p_num
                    CHANGING output.
  WRITE:/ output.
*&---------------------------------------------------------------------*
*&      Form  fibonacci
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fibonacci USING num
               CHANGING out.
  DATA: lv_num1 TYPE i,
        lv_num2 TYPE i,
        out1 TYPE i,
        out2 TYPE i.
  IF num > 2.
    lv_num1 = num - 1.
    PERFORM fibonacci USING lv_num1
                    CHANGING out1.
    lv_num2 = num - 2.
    PERFORM fibonacci USING lv_num2
                    CHANGING out2.
    out = out1 + out2.
  ELSE.
    out = num.
  ENDIF.
ENDFORM.                    " fibonacci

Post new comment