Home » 
        Kotlin » 
        Kotlin Programs » 
        Kotlin Basic Programs
    
    Kotlin program to display Fibonacci series
    
    
    
	
    
        Kotlin | Display Fibonacci Series:  Here, we are going to learn how to display the Fibonacci series in Kotlin programming language?
        
            Submitted by IncludeHelp, on April 22, 2020
        
    
    
    In mathematics, the Fibonacci series is the series of the numbers where each number is the sum of the two preceding numbers, starting from 0 and 1.
    Kotlin - Display Fibonacci series
    Given two initial terms term1 and term2, we have to display the Fibonacci series till N terms.
Example:
Input:
term1 = 0
term2 = 1
N = 10
Output:
Fibonacci series: 0 1 1 2 3 5 8 13 21 34
Input:
term1 = 0
term2 = 1
N = 5
Output:
Fibonacci series: 0 1 1 2 3
    Program to display Fibonacci series in Kotlin
/**
	* Display Fibonacci Series up to a Given number of terms
	* e.g.  0 1 1 2 3 5 8 13....n
*/
package com.includehelp.basic
import java.util.*
//Main Function entry Point of Program
fun main(args: Array<String>) {
	// Input Stream
	val scanner = Scanner(System.`in`)
	// input total number of terms
	println("Enter terms  : ")
	val n: Int = scanner.nextInt()
	var term1 = 0
	var term2 = 1
	var count = 1
	// Iterate Loop to print fibonacci Series upto given terms
	while (count <= n){
		print("$term1 ")
		val s = term1+term2
		term1 = term2;
		term2 = s
		count++
	}
}
Output
RUN 1:
Enter terms  : 
10
0 1 1 2 3 5 8 13 21 34 
---
Run 2:
Enter terms  : 
5
0 1 1 2 3
    
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement