Skip to content

Instantly share code, notes, and snippets.

@danielomiya
Last active February 22, 2021 23:34
Show Gist options
  • Select an option

  • Save danielomiya/5996b8b3debb525fb6631d080f37dc6a to your computer and use it in GitHub Desktop.

Select an option

Save danielomiya/5996b8b3debb525fb6631d080f37dc6a to your computer and use it in GitHub Desktop.
Recursive Fibonacci algorithm in C
#include <stdio.h>
#include <stdlib.h>
int fib(int n);
int main(int argc, char **argv) {
// compiling: gcc fib.c -o fib
// exec with: ./fib <n>
int i_num;
if (argc <= 1) {
printf("Usage: ./fib <n>\n");
return 1;
}
i_num = atoi(argv[1]);
printf("O %io. termo de fib eh %i\n", i_num, fib(i_num - 1));
return 0;
}
int fib(int n) {
// 0-indexed
if (n <= 1) return 1;
return fib(n - 2) + fib(n - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment