Fibonacci Series

fun main() {
    println("Enter a number")//1
    var t2=1//2
    var t1=0//3
    var nextTerm=0//4
    val number = readLine()!!.toInt()//5
    if(number<=1)//6
    {
        println(number)//7
    }
    else
    {
        for(i in 1..number)//8
        {
          print("$t1,")//9
          nextTerm = t1+t2//10
            t1=t2//11
            t2=nextTerm//11
        }
    }
}
  1. Instruct the Enter a number
  2. Declare and initialized t2 variable with value 1
  3. Declare and initialized t1 variable with the value 0
  4. Declare and initialized nextTerm variable with the value 0
  5. Convert the entered number to int and assign to number variable
  6. Check if Statement for entering number is less than and greater equal to 1
  7. Print the number on the console
  8. if Enter number is greater than 1 else block execute and here we will execute for loop from 1 to number
  9. Print the value of t1 on console
  10. Calculate the nextTerm value by added t1+t2
  11. Assign the value of t2 to t1
  12. Assign the value of nextTerm to t2

Output

Enter the size of the Array
5
10
45
67
9
90
Greater No of Array Element=90

Enter a number
10
0,1,1,2,3,5,8,13,21,34,

Leave a Reply

Your email address will not be published. Required fields are marked *