### Fibonacci Iterative and Recursive Algorithms

Write a program that neatly displays the first 21 numbers in the Fibonacci sequence in a table. Recall the definition for the Fibonacci sequence:

F0 = 0

F1 = 1

F2 = 1

Fn = Fn - 1 + Fn - 2

The program should calculate the numbers in the sequence using two different functions. In one display, use an iterative function and in the other use a recursive strategy. The driver code in main should do no calculations; instead, it should just draw the tables and populate them with well-formed calls to the two functions you write. Your functions should take a single integer argument and return the value of the appropriate term in the Fibonacci sequence using the argument as an index. For example Fibonacci(5) should return 5 because 5 is the fifth term in the Fibonacci sequence (it is really the 6th, but zero counts). Fibonacci(7) should return 13 because 13 is the 7th term in the Fibonacci sequence. i want to answer this question and code is same like that but something more specific answer for this question.please help us.
code is:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119`` ``````#include using namespace std; // x! = x * !(x - 1) int factorialIterative(int); int factorialRecursive(int); // f(0) = 0; f(1) = 1; f(2) = 1 // f(n) = f(n - 1) + f(n - 2) int fibonacciIterative(int); int fibonacciRecursive(int); int main() { int i = 0; for(i = 0; i < 10; i++) { cout << i << "! = " << factorialIterative(i) << endl; cout << i << "! = " << factorialRecursive(i) << endl; } return 0; } int factorialIterative(int factorialMe) { int i = 0; int tally = 1; if(factorialMe < 0) { cout << "NO ! DEFINED FOR " << factorialMe << endl; return -1; } else if(factorialMe == 0) { return 1; } else { for(i = factorialMe; i >= 1; i--) { tally *= i; } } return tally; } int factorialRecursive(int factorialMe) { // BASE CASES if(factorialMe < 0) { cout << "NO ! DEFINED FOR " << factorialMe << endl; return -1; } else if(factorialMe == 0) { return 1; } // RECURSIVE CASE else { return factorialMe * factorialRecursive(factorialMe - 1); } } // student implementation int fibonacciIterative(int fibMe); int fibonacciRecursive(int fibMe); ``````

"Us" is who?!

Here are 20 fibonacci numbers

0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181

The recursive function can be writtten the following way

 ``1234`` ``````unsigned long long fibonacciRecursive( unsigned int n ) { return ( ( n == 0 ) ? 0 : ( ( n == 1 ) ? 1 : fibonacciRecursive( n - 2 ) + fibonacciRecursive( n - 1 ) ) ); } ``````

I did not test it by the idea is correct.:)
Topic archived. No new replies allowed.