summaryrefslogtreecommitdiffstats
path: root/01-algorithmic_toolbox/01-intro/02-fibonacci_last_digit
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-11-13 20:29:59 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-13 20:29:59 +0100
commit0f13a35dee0df21f4c1a387b50582a958c7bc439 (patch)
treed5e7e2d59ea9a709b660d94428ea951a80f085ba /01-algorithmic_toolbox/01-intro/02-fibonacci_last_digit
parent13eab863735725f9a02cd4ae0b1c25725cc27569 (diff)
downloadcoursera-0f13a35dee0df21f4c1a387b50582a958c7bc439.zip
coursera-0f13a35dee0df21f4c1a387b50582a958c7bc439.tar.gz
Algorithms : complete 01-algorithmic_toolbox 01-intro
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.cpp19
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() {