summaryrefslogtreecommitdiffstats
path: root/01-algorithmic_toolbox/01-intro/01-fibonacci/fibonacci.cpp
blob: c2564b83885b30898a1213dc2d142b5bb9c72481 (plain)
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
#include <iostream>

int calc_fib(int n) {
    if (n <= 1)
        return n;

    return calc_fib(n - 1) + calc_fib(n - 2);
}

int fib(int n)
{
    int a, b, c, i;

    if (n <= 1)
        return n;

    a = 0; b = 1;
    for(i = 1; i < n; i++) {
        c = a + b;
        a = b;
        b = c;
    }

    return b;
}

int main() {
    int n = 0;
    std::cin >> n;

    std::cout << fib(n) << '\n';
    return 0;
}