Kotlin: Print Fibonacci Series

1. Introduction

In this blog post, we will learn how to write a Kotlin Program to Print the Fibonacci Series.

The Fibonacci Series is a sequence of numbers where each number is the sum of the two preceding ones. It usually starts with 0 and 1. In mathematical terms, the sequence F(n) is defined by the recurrence relation F(n)=F(n−1)+F(n−2), with seed values F(0)=0, F(1)=1. The series begins as: 0, 1, 1, 2, 3, 5, 8, 13, ... and so on. This sequence has wide applications in various fields, including mathematics and theoretical computer science.

2. Program Overview

Our Kotlin endeavor will:

1. Request the user to specify how many terms of the Fibonacci Series they wish to see.

2. Seize the provided number.

3. Generate and display the Fibonacci Series up to the specified number of terms.

3. Code Program

import java.util.Scanner

fun main() {
    // Instigate Scanner for assimilating user input
    val reader = Scanner(System.in)

    // Invite the user to enter the number of terms they want in the Fibonacci Series
    print("How many terms of the Fibonacci Series would you like to print? ")

    // Secure the provided number of terms
    val terms = reader.nextInt()

    // Ensure the user provides a positive number
    if (terms <= 0) {
        println("Please enter a positive integer.")
        return
    }

    // Print the Fibonacci Series
    println("Fibonacci Series:")
    for (i in 1..terms) {
        print(fibonacci(i).toString() + " ")
    }
}

// Recursive function to get the nth term in the Fibonacci Series
fun fibonacci(n: Int): Int {
    return if (n <= 1) n else fibonacci(n - 1) + fibonacci(n - 2)
}

Output:

How many terms of the Fibonacci Series would you like to print? 7
Fibonacci Series:
0 1 1 2 3 5 8 

4. Step By Step Explanation

1. Scanner Deployment: We employ the Scanner class from the java.util package to capture user input, represented by the reader instance.

2. Terms Collection: Through the print function, we ask the user how many terms of the Fibonacci Series they desire. Their input is then captured and stored in the terms variable.

3. Positive Terms Verification: Since we can't generate a negative number of terms, the program checks if the user's input is less than or equal to zero, issuing a prompt if this condition is met.

4. Fibonacci Series Generation: The loop then progresses through each term, invoking the fibonacci function to retrieve the value for the respective term and subsequently printing it. The fibonacci function itself is recursive: for each term, it adds the two preceding terms, unless the term is one of the first two, in which case it returns the term's value directly (0 for the first term and 1 for the second).

5. Displaying the Series: As the loop advances, each Fibonacci term gets printed, resulting in the complete series being displayed.

In this Kotlin showcase, we've bridged the gap between nature's enigmatic patterns and computational logic. Through recursion and loops, Kotlin serves as a potent tool to unravel and represent mathematical sequences and series.

Comments