-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC++ Recursion Fibonacci .cpp
More file actions
39 lines (33 loc) · 890 Bytes
/
C++ Recursion Fibonacci .cpp
File metadata and controls
39 lines (33 loc) · 890 Bytes
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
/**
[PROGRAM] : C++ / (Exercise) Fibonacci Sequence
[AUTHOR] : Saddam Arbaa
[Email] : <saddamarbaas@gmail.com> */
#include <iostream>
using namespace std;
// Function declaration
int fib(int n);
int main() // the Driver Code
{
int n, result; /* variables declaration */
do // get valid number
{
// asking valid positive number from user
cout << "Enter N --> N must be bigger than zero : ";
cin >> n;
}while(n <= 0);
result = fib(n); // call function
cout << result<<endl; // print the result
return 0;// signal to operating system everything works fine
}/** End of main function */
/** Recursive function to calculate Fibonacci Sequence
assuming that n is positive integer
*/
int fib(int n)
{
if(n <= 1) // base case
{
return n;
}
// Recursive Case
return fib(n - 1) + fib(n - 2);
}