diff --git a/algorithms/CPlusPlus/Maths/fibonacci-series.cpp b/algorithms/CPlusPlus/Maths/fibonacci-series.cpp new file mode 100644 index 00000000..ab9fe254 --- /dev/null +++ b/algorithms/CPlusPlus/Maths/fibonacci-series.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +class Fibonacci +{ + /** + * The Fibonacci Sequence is the series of numbers: + * + * 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... + * + * The next number is found by adding up the two numbers before it:, next number is the sum of previous two numbers + * for example 0, + * 1, 1, 2, 3, 5, 8, 13, 21 etc. + * The first two numbers of fibonacci series are 0 and 1. + * Time complexity: O(n) + * */ +public: + /* + * Example 1: the number of elements is introduced by the user via command prompt. + */ + void print_fibonacci_no_R() + { + int num1 = 0, num2 = 1, num3, i, number; + cout << "Enter the number of elements: "; + cin >> number; + cout << num1 << " " << num2 << " "; //printing 0 and 1 + + for (i = 2; i < number; ++i) //loop starts from 2 because 0 and 1 are already printed + { + num3 = num1 + num2; + cout << num3 << " "; + num1 = num2; + num2 = num3; + } + cout << "\n\n"; //printing 0 and 1 + } + + /* + * Example 2: the number of elements is introduced by the user via function input parameter. + */ + void printFibonacci_R(int n) + { + if (n > 2) + { // n finishes at 2 because 0 and 1 are already printed + if (n1 == 0) cout << n1 << " " << n2 << " "; //printing 0 and 1 + n3 = n1 + n2; + n1 = n2; + n2 = n3; + cout << n3 << " "; + printFibonacci_R(n - 1); + } + } + +private: + int n1{0}; + int n2{1}; + int n3; + +}; + + +int main() +{ + Fibonacci f; + f.print_fibonacci_no_R(); + f.printFibonacci_R(8); + return 0; +} \ No newline at end of file diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 64d91640..cc9cb207 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -78,3 +78,4 @@ 1. [Kaprekar Number](Maths/Kaprekar-number.cpp) 2. [Prime Number](Maths/prime-check.cpp) 3. [Prime Sieve](Maths/prime-sieve.cpp) +4. [Fibonacci Series](Maths/fibonaccci-series.cpp)