We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 109ded3 commit 8ad59b2Copy full SHA for 8ad59b2
src/c/Fibonacci.c
@@ -0,0 +1,31 @@
1
+#include <stdio.h>
2
+
3
+int fibonacci_iterative(int number) {
4
+ int last_number = 0;
5
+ int current_number = 1;
6
7
+ for (int index = 0; index < number; ++index) {
8
+ int temp = current_number;
9
+ current_number += last_number;
10
+ last_number = temp;
11
+ }
12
+ return last_number;
13
+}
14
15
+int fibonacci_recursive(int number) {
16
+ if (number == 0) {
17
+ return 0;
18
+ } else if (number == 1) {
19
+ return 1;
20
+ } else {
21
+ return fibonacci_recursive(number - 1) + fibonacci_recursive(number - 2);
22
23
24
25
+int main(void) {
26
+ int test_nbr = 12;
27
28
+ printf("iterative: %d\n", fibonacci_iterative(test_nbr));
29
+ printf("recursive: %d\n", fibonacci_recursive(test_nbr));
30
31
0 commit comments