Kotlin Program to find factorial of a number


In this program, You will learn how to find factorial of a number in Kotlin.


3! => 1 * 2 * 3 = 6

4! => 1 * 2 * 3 * 4 = 24

Example: How to find factorial of a number in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var num: Int
    var f = 1

    var sc = Scanner(System.`in`)

    print("Enter a number:")
    num = sc.nextInt()

    for (i in 1..num) {
        f *= i
    }

    println("Factorial is:$f")
}

Output:

Enter a number:4
Factorial is:24