Fibnocci Series in Kotlin
Published in
1 min readOct 7, 2019
Hey, Today we are going to learn Fibonocci series formula and code in Kotlin.
For Fobonocci Series, We have formula n = n-1 + n-2, means Our result value should be the sum of prevoius 2 terms.
lets have an example : 0 1 1 2 3 5 7 12 ………
Code :
fun printFibnoci(){
// n = n-1 + n-2
var a= 0
var b =1
var sum =0
for (i in 0 until 10){
sum = a+b
print (sum)
print(" ")
a= b
b= sum
}
}
We have assume a =0 && b=1 and use forloop till 10
We have taken 1 temporary variable known sum . First we do sum of previous 2 terms and then assign b variable value to a and sum value to b .
You can run this program once, and got the solution.