Skip to content

Commit

Permalink
Merge pull request OpenGenus#3728 from fuslonflare/factorial-kotlin
Browse files Browse the repository at this point in the history
Add Factorial in kotlin
  • Loading branch information
AdiChat authored Oct 6, 2018
2 parents 1f61173 + f9b6a3f commit 2154ace
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions code/mathematical_algorithms/src/factorial/Factorial.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.math.BigInteger
import java.util.*

/**
* Created by Phuwarin on 10/4/2018
* Part of Cosmos by OpenGenus
*/

object Factorial {

@JvmStatic
fun main(args: Array<String>) {
print("Enter a number: ")
val enterNum = Scanner(System.`in`)
val n = enterNum.nextLong()
println("Factorial is: " + factorial(n))
}

private fun factorial(n: Long): BigInteger {
var result = BigInteger.ONE
for (i in 1..n) {
result = result.multiply(BigInteger.valueOf(i))
}
return result
}
}

0 comments on commit 2154ace

Please sign in to comment.