forked from OpenGenus/cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request OpenGenus#3728 from fuslonflare/factorial-kotlin
Add Factorial in kotlin
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |