# Kotlin Program to Generate And Print Fibonacci Series

In this post, we will go through Kotlin program to generate and print fibonacci series. We will use for loop, while loop for this task.

###### What is Fibonacci series?

Fibonacci series is a series where each term is sum of previous two terms (except first and second terms. First term is 0 and second term is 1)
For example,

```0  1  1  2  3  5  8  13  21  34  55  89  144  233 .....
```

Series goes on infinitely….

You can write this fibonacci series in Kotlin program as well.

We will –

## 1. Generate fibonacci series using for Loop

In this section, we will learn to generate fibonacci series upto given number of terms using for loop.

Sourcecode –

```import java.util.*

fun main() {

println("Enter number of terms:")

var t1 = 0
var t2 = 1

println("\nFibonacci series of first \$number terms: ")

for (index in 1..number) {
print("\$t1  ")

val sum = t1 + t2
t1 = t2
t2 = sum
}

println("\n\nThe End")
}
```

When you run the program, output will be

```Enter number of terms:
20

Fibonacci series of first 20 terms:
0  1  1  2  3  5  8  13  21  34  55  89  144  233  377  610  987  1597  2584  4181

The End
```

Here, we have created an object of Scanner. Scanner takes an argument which says where to take input from.
System.`in` means take input from standard input – Keyboard.

read.nextIn() means read anything entered by user before space or line break from standard input – Keyboard.
Input read by scanner is then stored in variable number

Let’s assume user has entered 4. So, fibonacci series for first 4 numbers will be generated.
We are running a for loop from 1 to given number (in this case, it’s 4)

index is a local variable to for loop.
Inside for loop, we print value of t1. So, current value of t1 is value at current index in fibonacci sequence.

At index = 1, t1 = 0, t2 = 1.
Print value of t1.
Define a variable sum that stores sum of t1 and t2.
Now, store value of t2 into t1.
Then, store value of sum into t1.

So, index = 2, t1 = 1, t2 = 1
Print value of t1.
Find sum of t1 and t2 and store it into sum.
Now, store value of t2 into t1.
Then, store value of sum into t1.

So, index = 3, t1 = 1, t2 = 2
Print value of t1.
Find sum of t1 and t2 and store it into sum.
Now, store value of t2 into t1.
Then, store value of sum into t1.

So, index = 4, t1 = 2, t3 = 3
Print value of t1.
Find sum of t1 and t2 and store it into sum.
Now, store value of t2 into t1.
Then, store value of sum into t1.

That’s it. for loop will be terminated after this iteration.

## 2. Generate fibonacci series using while Loop

In previous section, we used for loop to generate fibonacci series. In this section, we will use while loop
to generate this fibonacci sequence in kotlin.

Sourcecode –

```import java.util.*

fun main() {

println("Enter number of terms:")

var t1 = 0
var t2 = 1

println("\nFibonacci series of first \$number terms: ")

var index = 1
while (index <= number) {
print("\$t1  ")

val sum = t1 + t2
t1 = t2
t2 = sum

index++
}

println("\n\nThe End")
}
```

When you run the program, output will be

```Enter number of terms:
12

Fibonacci series of first 12 terms:
0  1  1  2  3  5  8  13  21  34  55  89

The End
```

Here, we have created an object of Scanner. Scanner takes an argument which says where to take input from.
System.`in` means take input from standard input – Keyboard.

read.nextIn() means read anything entered by user before space or line break from standard input – Keyboard.
Input read by scanner is then stored in variable number

Let’s assume user has entered 4. So, fibonacci series for first 4 numbers will be generated.
We are running a while loop from 1 to given number (in this case, it’s 4)

index is a variable being used to decide whether code inside while loop should be executed or not.

Inside while loop, we print value of t1. So, current value of t1 is value at current index in fibonacci sequence.

At index = 1, t1 = 0, t2 = 1.
Print value of t1.
Define a variable sum that stores sum of t1 and t2.
Now, store value of t2 into t1.
Then, store value of sum into t1.
Then, increase value of index by 1.

So, index = 2, t1 = 1, t2 = 1
Print value of t1.
Find sum of t1 and t2 and store it into sum.
Now, store value of t2 into t1.
Then, store value of sum into t1.
Then, increase value of index by 1.

So, index = 3, t1 = 1, t2 = 2
Print value of t1.
Find sum of t1 and t2 and store it into sum.
Now, store value of t2 into t1.
Then, store value of sum into t1.
Then, increase value of index by 1.

So, index = 4, t1 = 2, t3 = 3
Print value of t1.
Find sum of t1 and t2 and store it into sum.
Now, store value of t2 into t1.
Then, store value of sum into t1.
Then, increase value of index by 1.

At index = 5, condition 5 <=4 becomes false. So, while loop is terminated.

### 3. Generate Fibonacci Series Upto Given Number

Now, we will see how to generate fibonacci series upto given number.

Sourcecode –

```import java.util.*

fun main() {

println("Enter number:")

var t1 = 0
var t2 = 1

println("\nFibonacci series upto \$number: ")

while (t1 <= number) {
print("\$t1  ")

val sum = t1 + t2
t1 = t2
t2 = sum
}

println("\n\nThe End")
}
```

When you run the program, output will be –

``` Enter number:
4234

Fibonacci series upto 4234:
0  1  1  2  3  5  8  13  21  34  55  89  144  233  377  610  987  1597  2584  4181

The End
```

Here, Everything is same as explained above. We just modified the condition inside while block.

Now, code inside while block will be executed only if value of t1 is less than value of number.

Thus, we went through Kotlin program to generate and print fibonacci series