0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Print Fibonacci Series

You can print the Fibonacci series in Kotlin using a loop like this: var a = 0; var b = 1; for (i in 1..n) { print("$a "); val sum = a + b; a = b; b = sum; } where n is the number of terms.
📋

Examples

Input5
Output0 1 1 2 3
Input1
Output0
Input0
Output
🧠

How to Think About It

To print the Fibonacci series, start with the first two numbers 0 and 1. Then, keep adding the last two numbers to get the next one. Repeat this process until you have printed the desired number of terms.
📐

Algorithm

1
Get the number of terms to print, n.
2
Initialize two variables a = 0 and b = 1 for the first two Fibonacci numbers.
3
Loop from 1 to n:
4
Print the current number a.
5
Calculate the next number as sum = a + b.
6
Update a to b and b to sum.
7
End loop.
💻

Code

kotlin
fun main() {
    val n = 10
    var a = 0
    var b = 1
    for (i in 1..n) {
        print("$a ")
        val sum = a + b
        a = b
        b = sum
    }
}
Output
0 1 1 2 3 5 8 13 21 34
🔍

Dry Run

Let's trace printing 5 Fibonacci numbers through the code

1

Initialize

a = 0, b = 1, n = 5

2

Iteration 1

Print a=0; sum=0+1=1; a=1; b=1

3

Iteration 2

Print a=1; sum=1+1=2; a=1; b=2

4

Iteration 3

Print a=1; sum=1+2=3; a=2; b=3

5

Iteration 4

Print a=2; sum=2+3=5; a=3; b=5

6

Iteration 5

Print a=3; sum=3+5=8; a=5; b=8

Iterationa (printed)suma (updated)b (updated)
10111
21212
31323
42535
53858
💡

Why This Works

Step 1: Start with first two numbers

We begin with a = 0 and b = 1 because the Fibonacci series starts with 0 and 1.

Step 2: Print current number

In each loop, we print the current number a which represents the current Fibonacci term.

Step 3: Calculate next number

We find the next Fibonacci number by adding a + b, then update a and b to move forward in the series.

🔄

Alternative Approaches

Recursive approach
kotlin
fun fibonacci(n: Int): Int {
    return if (n <= 1) n else fibonacci(n - 1) + fibonacci(n - 2)
}

fun main() {
    val n = 10
    for (i in 0 until n) {
        print("${fibonacci(i)} ")
    }
}
Simple but inefficient for large n due to repeated calculations.
Using sequence generator
kotlin
fun fibonacciSequence(n: Int) = sequence {
    var a = 0
    var b = 1
    repeat(n) {
        yield(a)
        val sum = a + b
        a = b
        b = sum
    }
}

fun main() {
    fibonacciSequence(10).forEach { print("$it ") }
}
More Kotlin-idiomatic and lazy evaluation, good for large or infinite sequences.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs n times, each time doing constant work, so time is O(n).

Space Complexity

Only a few variables are used regardless of n, so space is O(1).

Which Approach is Fastest?

The iterative loop is fastest and most memory efficient compared to recursion which has exponential time and sequence which uses lazy evaluation but more overhead.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)Simple and fast for all n
RecursiveO(2^n)O(n)Small n, easy to understand
Sequence generatorO(n)O(1)Kotlin idiomatic, lazy evaluation
💡
Use variables to hold the last two Fibonacci numbers and update them in each loop to avoid extra memory.
⚠️
Beginners often forget to update both variables correctly, causing an infinite loop or wrong sequence.