With experience of java programming, I started learning Kotlin today. I am playing with the language and found me stuck to find a factorial using for loop in kotlin. I managed to do this using while loop.
import java.util.Scanner
fun main(args: Array<String>){
val reader=Scanner(System.`in`)
val x: Int = reader.nextInt()
println(factorial(x))
}
fun factorial(a: Int): Int{
var ans=1
var i: Int = a
while(i>1){
ans*=i
i--
}
return ans
}
Please help me to do this using a for loop.
Thanks