Advertisement
volkovich_maksim

fibonacci recursive

Sep 10th, 2016
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. long int fib(int n)
  4. {
  5.    long int f=0;
  6.    if (n == 0)
  7.       f = 0;
  8.    else if (n == 1)
  9.       f = 1;
  10.    else if (n >= 2)
  11.       f = fib(n-1)+fib(n-2);
  12.    return f;
  13. }
  14.  
  15. int main(void)
  16. {  
  17.    int n;
  18.    int qualifier = 1;
  19.    while (qualifier == 1 || qualifier == 0)
  20.    {
  21.       qualifier = scanf("%d", &n);
  22.       if (qualifier == 1 && n >= 0)
  23.          {
  24.             printf("%ld\n", fib(n));
  25.          }
  26.       if (qualifier == 1 && n < 0)
  27.          {  
  28.             printf("wrong type\n");
  29.          }
  30.    }
  31.    return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement