kotlin 尾递归阶乘
Factorial of number is the product of all positive numbers less or equal to the number.
数字阶乘是所有小于或等于该数字的正数的乘积。
Factorial of number n is denoted by n! is calculated using the formula, n! = n*(n-1)*(n-2)*...*2*1.
n的阶乘由n! 使用公式n计算! = n *(n-1)*(n-2)* ... * 2 * 1 。
To find the factorial of the entered number, we have used a for loop that runs from n to 1. At each iteration, i is multiplied with the factorial variable.
为了找到输入数字的阶乘 ,我们使用了一个从n到1的for循环。 在每次迭代中, i乘以阶乘变量。
Example:
例:
Input:
n = 5
Output:
factorial = 120 [5x4x3x2x1 = 120]
在Kotlin查找数字阶乘的程序 (Program to find factorial of a number in Kotlin)
package com.includehelp.basic
import java.util.*
// Main Method Entry Point of Program
fun main(args:Array<String>) {
// InputStream to get Input
var reader = Scanner(System.`in`)
// Input Integer Value
println("Enter Number : ")
var number = reader.nextInt()
var factorial: Long=1
for(i in 1..number){
// Calculate Factorial
factorial*=i.toLong()
}
// printing
println("Factorial of $number is $factorial ")
}
Output
输出量
Run1 :
Enter Number :
5
Factorial of 5 is 120
---
Run 2:
Enter Number :
12
Factorial of 12 is 479001600
翻译自: https://www.includehelp.com/kotlin/find-factorial-of-a-number.aspx
kotlin 尾递归阶乘