Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DZone's Guide to

# Finding Fibonacci With Both Simple Logic and Mathematical Formula in C

·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

To find Fibonacci sequence, mathematically there're several ways Like Recursif function and using the Golder Ratio or X^2 - X - 1 = 0 Equation.

Above is a recursif function which can be calculated like this

According to the formula, we have initial values for F(0) = 0, F(1) =1

So for n > 1 , we have smth like this:

```F(2) = F(2-1) + F(2-2)
= F(1)   + F(0)
= 1 + 0 = 1
ans so on....for the next n so it will finally form the sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
```

but mosth people dont like Recurif.

We have another Options to do that

1. Use just a simple Logic (see the code)

2. Or use the formula

Fibonacci numbers can also be found by this formula

Fn = (x1n – x2n) / root(5)  , where Fn is rounded down
Where X1 and X2 are roots of the    X^2    - X - 1 = 0  Equation
To find X1 and X2 , we can use this formula

So that X1,X2  =>
then we only need to form the formula into Code
for each X1, X2 , we can set variables

float x1 = ( 1 + sqrt(5) ) / 2;

float x2 = (1 - sqrt(5) ) / 2;

That's it.

```1. Simple Logic

int fibonaci(int n){

int i,next,a=0,b=1;
for(i=0;i
```
Topics:

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Opinions expressed by DZone contributors are their own.