fib.c 343 B

12345678910111213141516171819202122232425
  1. #include <stdio.h>
  2. #include <stdlib.h> // atoi()
  3. int fib(n)
  4. {
  5. if (n <= 2)
  6. return 1;
  7. else
  8. return fib(n-1) + fib(n-2);
  9. }
  10. int main(int argc, char **argv)
  11. {
  12. int n;
  13. if (argc < 2) {
  14. printf("usage: fib n\n"
  15. "Compute nth Fibonacci number\n");
  16. return 1;
  17. }
  18. n = atoi(argv[1]);
  19. printf("fib(%d) = %d\n", n, fib(n));
  20. return 0;
  21. }