-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathfibonacci.cpp
49 lines (37 loc) · 1.02 KB
/
fibonacci.cpp
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
/* This program prints the nth term in the fibonacci sequence. Recursive method
is used here. Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)*/
#include <iostream>
using namespace std;
// This function returns the nth terms in fibonacci sequence
int fibonacci(int number) {
// Base case for n = 0 and n = 1
if(number == 0)
return 0;
else if(number == 1)
return 1;
// Recursively call the function for n-1 and n-2
return fibonacci(number - 1) + fibonacci(number - 2);
}
int main() {
// Take number as input from the user
int number;
cout << "Enter a number: ";
cin >> number;
// If number is less than 0, exit
if(number < 0) {
cout << "\nPlease enter a non-negative number." << endl;
exit(0);
}
// Else call the function and print the nth term
cout << "\nThe term at index " << number << " of the fibonacci sequence is " << fibonacci(number) << endl;
return 0;
}
/*
Sample I/O:
1)
Enter a number: 7
The term at index 7 of the fibonacci sequence is 13
2)
Enter a number: -23
Please enter a non-negative number.
*/