Hi, Welcome to Proto Coders Point, In this dart programming tutorial we will learn what is fibonacci number & write a dart program to generate fibonacci sequence number from 0 – N. It is one of the basic program every programming should know it.

## What is fibonacci sequence number?

In mathematic, the fibonacci series, is denoted by F_{n}.

Here, the next number will be the sum of previous two numbers.

Initial starting two numbers will be 0 & 1, denoted by F_{0} = 0, F_{1} = 1;

Simple mathematical formula to get Fn ‘ the next number’..

F_{n}= F_{(n-1)}+F_{(n-2)}

Therefore, the fib sequence looks like this:-

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ........and so on up to N^{th}

## How is fibonacci numbers generated?

Initially we have starting number which always start with 0 & 1 i.e F_{0} = 0, F_{1} = 1;

Now we perform sum of F_{0} + F_{1} = F_{n}.

**i.e**. F_{2 }= F_{0} + F_{1}.

**Fibonacci Sequence flow animation example**

Eg: initial value 0 & 1.

1_{st} iteration: F_{2}= F_{0 }+ F_{1}

0 + 1

F_{2} = 1

2_{nd} iteration: F_{3}= F_{1 }+ F_{2}

1 + 1

F_{3} = 2

3_{rd} iteration: F_{4}= F_{2 }+ F_{3}

1 + 2

F_{4} = 3

4_{th} iteration: F_{5}= F_{3 }+ F_{4}

2 + 3

F_{5} = 5

5_{th} iteration: F_{6}= F_{4 }+ F_{5}

3 + 5

F_{6} = 8

and so on up to N_{th}

## How to implement fibonacci in dart? Fibonacci Algorithm.

Step1: Start

Step2: Declare initial variable f0 = 0, f1 = 1,f2, n, i =1;

Step3: Read n from user, how many fibo sequence user wants to generate.

Step4: Print f0 & f1

Step5: Repeat until i < n

5.1: f2 = f0 + f1;

5.2: Print f2;

5.3: inter change value f0 = f1;

f1 = f2;

5.4: i ++;

Step6: Stop.

## Fibonacci program using dart

**Fibonacci program using loop**

void main() { int n1=0,n2=1,n3; print(n1); print(n2); for(int i = 2 ;i <= 16; i++){ n3 = n1+n2; print('$n3'); n1 = n2; n2 = n3; } }

### Fibonacci program using recursion

int fibonacci(int n) => n <= 2 ? 1 : fibonacci(n - 2) + fibonacci (n - 1); main() { String output = ""; for (int i = 1; i <= 16; ++i) { output += fibonacci(i).toString() + ", "; } print(output + "..."); }

**output**

## What is a real world example of the Fibonacci numbers?

One of the best & easiliest way to understand fibonacci series used in real file application is, converting **mile to kilometer** and **kilometer to mile**.

Let’s now see the Fibonacci series :

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,…

For example :

**Mile to kilometer conversion**: If we take a number from Fibonacci series i.e., 8 then the kilometer value will be 12.874752 by formulae, which is nearly 13 by rounding.**Kilometer to mile conversion**: If we take a number from Fibonacci series i.e., 89 as kilometer value then mile value will be 55.30203610912272 by formulae, which could be considered as 55 by rounding.

Finally in both conversions, the left element will be considered as **mile value** and right element will be considered as **Kilometer value** in the ** Fibonacci series**.