Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int fib(int x)
- {
- if (x == 1)
- return 0;
- else if (x <= 0)
- return -1;
- else if (x == 2)
- return 1;
- else
- return fib(x - 1) + fib(x - 2);
- }
- int rec(int r)
- {
- int a = 0;
- int b = 1;
- int c = 0;
- if (r < 1) {
- c = -1;
- }
- else if (r == 2) {
- c = 1;
- }
- for (int i = 2; i < r; i++) {
- c = a + b;
- a = b;
- b = c;
- }
- return c;
- }
- int main()
- {
- int n, x, r;
- printf("Zadejte hledany clen Fibonacciho posloupnosti: ");
- scanf_s("%d", &n);
- x = n;
- r = n;
- printf("%d. clen Fibonacciho posloupnosti je : %d\n", n, fib(x));
- printf("%d. clen Fibonacciho posloupnosti je : %d\n", n, rec(r));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement