COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Recursion Factorial Program Fibonacci Program Integer Power Program Tower of Hanoi Program Max Element Program Palindrome Program Reverse String Program Array Sum Program



FibonacciWithRecursion Program


Description

The Fibonacci sequence of numbers start with 0 and 1, then the following number is the addition of two previous numbers.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144, 233, 377,...

This program inputs an index (position) and then calculates the Fibonacci number at this index using recursion technique.

Associated Concepts:

Recursion

Algorithm:

main method:

Declare variables.

Input an integer index from the keyboard.

Call the recursive method fibonacci() with index as the argument.

Print the result.

fibonacci method definition:

If the method is call with the base case: index is 0 or index is 1, it returns the result.

Otherwise, the method returns fibonacci(index - 1) + fibonacci(index - 2).

Source Code



Sample Run



Video