diff options
Diffstat (limited to '01-algorithmic_toolbox/01-intro/02-fibonacci_last_digit')
-rw-r--r-- | 01-algorithmic_toolbox/01-intro/02-fibonacci_last_digit/fibonacci_last_digit.cpp | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/01-algorithmic_toolbox/01-intro/02-fibonacci_last_digit/fibonacci_last_digit.cpp b/01-algorithmic_toolbox/01-intro/02-fibonacci_last_digit/fibonacci_last_digit.cpp index 08e8dd8..f064113 100644 --- a/01-algorithmic_toolbox/01-intro/02-fibonacci_last_digit/fibonacci_last_digit.cpp +++ b/01-algorithmic_toolbox/01-intro/02-fibonacci_last_digit/fibonacci_last_digit.cpp @@ -1,8 +1,23 @@ #include <iostream> int get_fibonacci_last_digit(int n) { - //write your code here - return 0; + int a, b, c, i; + + if (n <= 1) + return n; + + a = 0; b = 1; + for(i = 1; i < n; i++) { + c = a + b; + if (c >= 10) + c -= 10; + a = b; + b = c; + } + if (b >= 10) + b -= 10; + + return b; } int main() { |