0% found this document useful (0 votes)
9 views1 page

FibonacciNumber Function

Uploaded by

omurabuyousuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

FibonacciNumber Function

Uploaded by

omurabuyousuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

using namespace std;

//Iterative Approach

int fibonacci_iterative(int n) {
if (n <= 1) {
return n; // Base cases: F(0) = 0, F(1) = 1
}
int prev = 1, curr = 0, next;
for (int i = 2; i <= n; i++) {
next = prev + curr;
prev = curr;
curr = next;
}
return curr;
}

int main() {
int n;
cout << "Enter the Fibonacci term you want to find: ";
cin >> n;
cout << "Fibonacci(" << n << ") = " << fibonacci_iterative(n) << endl;
return 0;
}

You might also like