Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int fibonacci(int n) {
- // Exit the program if n is negative
- if (0 > n) exit(1);
- switch (n) {
- // Special cases n = 0 and n = 1
- case 0:
- case 1:
- return n;
- break;
- default:
- // Fibonacci formula for n > 1
- return fibonacci(n - 1) + fibonacci(n - 2);
- }
- }
- int main() {
- // Loop for printing the first 6 numbers of the succession
- for (int i = 0; 6 > i; i++) {
- printf("%i ", fibonacci(i));
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement