Menu
Coddy logo textTech

Recap - Factorial

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 53 of 93.

challenge icon

Challenge

Medium

Write a function factorial that takes n and returns the factorial of that number.

Use a loop to multiply all positive integers from 1 to n together.

Parameters:

  • n (Int): A positive integer (1 or greater)

Returns: The factorial of n (Int)

Input constraints: The input satisfies 1 <= n <= 12, so the factorial fits in Int.

Try it yourself

fun factorial(n: Int): Int {
    // Write code here
}

All lessons in Fundamentals

Practice on your own: Kotlin playground