Advertisement
Shailrshah

Fibonacci Recurrence

Nov 30th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.35 KB | None | 0 0
  1. #include <stdio.h>
  2. int fibonacci(int n){
  3.     if (n == 0 || n == 1) return n;
  4.     else return fibonacci(n - 1) + fibonacci(n - 2); //Recursion until n becomes 0 or 1
  5. }
  6. int main(){
  7.     int n;
  8.     printf("Enter a positive number: ");
  9.     scanf("%d", &n);
  10.     if(n > 0) printf("Term number %d is %d.", n, fibonacci(n-1));
  11.     else printf("\nInvalid choice.");
  12.     return 0;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement