If you’ve just started learning Kotlin and want to practice loops in a real-world example, generating the Fibonacci series is a perfect choice. It’s simple enough to grasp, yet teaches you how to handle variables, loops, and logic in Kotlin efficiently.
In this guide, we’ll explore Fibonacci Using Loops in Kotlin, break down the code step-by-step, and keep it beginner-friendly — without skipping important details.
What is the Fibonacci Sequence?
The Fibonacci sequence is a series of numbers where each number is the sum of the two before it.
It starts like this:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
Mathematically:
F(n) = F(n-1) + F(n-2)
Where:
F(0) = 0
F(1) = 1
Why Use Loops Instead of Recursion?
While recursion can generate Fibonacci numbers, it’s less efficient for large sequences because:
- It repeats calculations unnecessarily.
- It uses more memory due to function calls.
Using loops in Kotlin:
Saves memory.
Runs faster.
Easier to understand for beginners.
That’s why Fibonacci Using Loops in Kotlin is both simple and efficient.
Kotlin Program for Fibonacci Using Loops
Here’s the complete Kotlin code:
fun main() {
val terms = 10 // Number of Fibonacci numbers to print
var first = 0
var second = 1
println("Fibonacci Series using loops:")
for (i in 1..terms) {
print("$first ")
// Calculate the next number
val next = first + second
first = second
second = next
}
}
Step-by-Step Code Explanation
Let’s break it down so you truly understand:
1. Declaring Variables
fun main() {
val terms = 10 // Number of Fibonacci numbers to print
var first = 0
var second = 1
println("Fibonacci Series using loops:")
for (i in 1..terms) {
print("$first ")
// Calculate the next number
val next = first + second
first = second
second = next
}
}
terms
→ how many numbers you want to print.first
andsecond
→ the first two Fibonacci numbers.
2. Using a Loop
for (i in 1..terms) {
print("$first ")
val next = first + second
first = second
second = next
}
- Loop runs from 1 to
terms
→ controls how many numbers are printed. print("$first ")
→ displays the current number.val next = first + second
→ calculates the next Fibonacci number.
We then shift the values:
first
becomes the oldsecond
.second
becomes the newnext
.
Not clear — let’s dry run it for better understanding.
Iteration 1 (i = 1)
- Print
first
→ prints0
next = first + second = 0 + 1 = 1
- Update:
first = second = 1
second = next = 1
Output: 0
Iteration 2 (i = 2)
- Print
first
→ prints1
next = 1 + 1 = 2
- Update:
first = 1
second = 2
Output: 0 1
Iteration 3 (i = 3)
- Print
first
→ prints1
next = 1 + 2 = 3
- Update:
first = 2
second = 3
Output: 0 1 1
Iteration 4 (i = 4)
- Print
first
→ prints2
next = 2 + 3 = 5
- Update:
first = 3
second = 5
Output: 0 1 1 2
Iteration 5 (i = 5)
- Print
first
→ prints3
next = 3 + 5 = 8
- Update:
first = 5
second = 8
Output: 0 1 1 2 3
Iteration 6 (i = 6)
- Print
first
→ prints5
next = 5 + 8 = 13
- Update:
first = 8
second = 13
Output: 0 1 1 2 3 5
Iteration 7 (i = 7)
- Print
first
→ prints8
next = 8 + 13 = 21
- Update:
first = 13
second = 21
Output: 0 1 1 2 3 5 8
Iteration 8 (i = 8)
- Print
first
→ prints13
next = 13 + 21 = 34
- Update:
first = 21
second = 34
Output: 0 1 1 2 3 5 8 13
Iteration 9 (i = 9)
- Print
first
→ prints21
next = 21 + 34 = 55
- Update:
first = 34
second = 55
Output: 0 1 1 2 3 5 8 13 21
Iteration 10 (i = 10)
- Print
first
→ prints34
next = 34 + 55 = 89
- Update:
first = 55
second = 89
Output: 0 1 1 2 3 5 8 13 21 34
Final Output
If terms = 10
, output will be:
Fibonacci Series using loops:
0 1 1 2 3 5 8 13 21 34
Tips to Make It Even Better
- User Input: Instead of hardcoding
terms
, ask the user how many numbers they want.
print("Enter the number of terms: ")
val n = readLine()!!.toInt()
- Formatting: Add commas or line breaks for readability.
- Performance: This loop method already runs in O(n) time, making it efficient even for large
terms
.
Why This Approach Works Well
The Fibonacci Using Loops in Kotlin approach is ideal for:
- Beginners learning loops.
- Anyone needing quick and efficient Fibonacci generation.
- Avoiding recursion stack overflow for large sequences.
It’s clean, easy to debug, and performs well.
Conclusion
The Fibonacci sequence is a timeless example for learning programming logic. By using loops in Kotlin, you get the perfect balance between simplicity and efficiency. Whether you’re practicing for interviews or just improving your coding skills, this method will serve you well.
Next time you think about Fibonacci, remember — you don’t always need recursion. A good old loop can do the job beautifully.