Q:

Kotlin program to display Fibonacci series

0

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.

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now