forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.cpp
More file actions
32 lines (24 loc) · 753 Bytes
/
fibonacci.cpp
File metadata and controls
32 lines (24 loc) · 753 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
#include <iostream> //to get the input and print the output
#include <stdint.h>
using std::cin;
using std::cout;
//returns fibonacci number for specified position
int64_t getFibonnaci(unsigned int input){
//base case
if (input <= 1){
return input;
}
//recursively calls getFibonnaci function to get the previous fibonacci numbers until base case is reached
//all the previous fibonacci numbers are then added
//and returns the fibonacci number for current position
return getFibonnaci(input-1) + getFibonnaci(input-2);
}
int main(){
int userInput;
//gets user Input
cout << "Enter a number : \n";
cin >> userInput;
cout << "The fibonacci number for the sepcified position is: ";
cout << getFibonnaci(userInput);
return 0;
}