Advertisement
Guest User

Fibonacci advanced version by Alejandro Piqueras

a guest
Jul 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int fibonacci(int n) {
  5.     // Exit the program if n is negative
  6.     if (0 > n) exit(1);
  7.     switch (n) {
  8.         // Special cases n = 0 and n = 1
  9.         case 0:
  10.         case 1:
  11.             return n;
  12.             break;
  13.         default:
  14.             // Fibonacci formula for n > 1
  15.             return fibonacci(n - 1) + fibonacci(n - 2);
  16.     }
  17. }
  18.  
  19. int main() {
  20.     // Loop for printing the first 6 numbers of the succession
  21.     for (int i = 0; 6 > i; i++) {
  22.         printf("%i ", fibonacci(i));
  23.     }
  24.     printf("\n");
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement