kotlin 尾递归阶乘_Kotlin程序查找数字的阶乘

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.

为了找到输入数字的阶乘 ,我们使用了一个从n1的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 尾递归阶乘

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值