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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>

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);

 

please help us.
"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

1
2
3
4
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.