31,659 views
33 votes
33 votes
100 points if you give a solution to this question.

write a Fibonacci Algorithm (or a program using a C programming language) to calculate the nth (number n). of the Fibonacci sequence.
notice that U(0)=0 ; U(1)=1 ; U(n)=U(n-1)+U(n-2)

User Ziwon
by
2.5k points

2 Answers

8 votes
8 votes

Answer:

Fn=(Fn−1+Fn−2) F n = ( F n − 1 + F n − 2 ) , where n >1.

Step-by-step explanation:

User Broot
by
3.5k points
11 votes
11 votes

Answer: Here is the solution in C. This works programmatically. Feel free to alter it if there are any garbage collection issues.

Step-by-step explanation:

#include <stdio.h>

int fib(int num) {

if (num <= 0) return 0;

else if (num == 1) return 1;

int fst = 0;

int snd = 1;

int sum = fst + snd;

for (int n = 0; n < num; ++n) {

sum = fst + snd;

fst = snd;

snd = sum;

}

return fst;

}

int main() {

printf("%d", fib(5));

return 0;

}

User Roy Longbottom
by
2.7k points