Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int recursiveFib(int n) {
- if (n <= 1) return n;
- else return recursiveFib(n - 1) + recursiveFib(n - 2);
- }
- int iterativeFib(int n) {
- int f1 = 0;
- int f2 = 1;
- int temp;
- for (int i = 0; i < n; i++) {
- temp = f1;
- f1 = f2;
- f2 += temp;
- }
- return f1;
- }
- int main() {
- int n;
- cin >> n;
- cout << "Recursive Fib: " << recursiveFib(n) << endl;
- cout << "Iteraive Fib: " << iterativeFib(n) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement