Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int fib(int x)
  5. {
  6. if (x == 1)
  7. return 0;
  8. else if (x <= 0)
  9. return -1;
  10. else if (x == 2)
  11. return 1;
  12. else
  13. return fib(x - 1) + fib(x - 2);
  14. }
  15.  
  16. int rec(int r)
  17. {
  18. int a = 0;
  19. int b = 1;
  20. int c = 0;
  21. if (r < 1) {
  22. c = -1;
  23. }
  24. else if (r == 2) {
  25. c = 1;
  26. }
  27. for (int i = 2; i < r; i++) {
  28. c = a + b;
  29. a = b;
  30. b = c;
  31. }
  32. return c;
  33. }
  34.  
  35. int main()
  36. {
  37. int n, x, r;
  38. printf("Zadejte hledany clen Fibonacciho posloupnosti: ");
  39. scanf_s("%d", &n);
  40. x = n;
  41. r = n;
  42. printf("%d. clen Fibonacciho posloupnosti je : %d\n", n, fib(x));
  43. printf("%d. clen Fibonacciho posloupnosti je : %d\n", n, rec(r));
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement