Given a positive integer number, print a list of Fibonacci sequence up to the nth term. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, often starting with 0 and 1. For example, the Fibonacci sequence up to 7th term is: 0,1,1,2,3,5,8. The input is the term n and the output should be the sequential integers of Fibonacci as a string, separated by a comma and space.
While loops
Generate the Fibonacci sequence upto nth term
Report a typo
Sample Input 1:
5Sample Output 1:
0, 1, 1, 2, 3Sample Input 2:
7Sample Output 2:
0, 1, 1, 2, 3, 5, 8Write a program in Kotlin
// import required libraries
import java.util.*
// main function
fun main(args: Array<String>) {
val scanner = Scanner(System.`in`)
// take input
val n = scanner.nextInt()
// initialize the first two integers of the Fibonacci series to 0 and 1 respectively
var t1 = 0
var t2 = 1
// Write your "while" loop code here to complete the series
// print the series
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.