0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Find Factorial of a Number

You can find the factorial of a number in Kotlin by using a loop like for (i in 1..n) result *= i to multiply all numbers from 1 to n.
📋

Examples

Input0
OutputFactorial of 0 is 1
Input5
OutputFactorial of 5 is 120
Input10
OutputFactorial of 10 is 3628800
🧠

How to Think About It

To find the factorial of a number, multiply all whole numbers from 1 up to that number. If the number is 0, the factorial is 1 by definition. We use a loop to multiply these numbers step by step.
📐

Algorithm

1
Get the input number n
2
If n is 0, return 1 as factorial
3
Initialize result as 1
4
For each number i from 1 to n, multiply result by i
5
Return the result as factorial
💻

Code

kotlin
fun main() {
    val n = 5
    var factorial = 1L
    for (i in 1..n) {
        factorial *= i
    }
    println("Factorial of $n is $factorial")
}
Output
Factorial of 5 is 120
🔍

Dry Run

Let's trace the factorial calculation for n=5 through the code

1

Initialize

n = 5, factorial = 1

2

Multiply by 1

factorial = 1 * 1 = 1

3

Multiply by 2

factorial = 1 * 2 = 2

4

Multiply by 3

factorial = 2 * 3 = 6

5

Multiply by 4

factorial = 6 * 4 = 24

6

Multiply by 5

factorial = 24 * 5 = 120

ifactorial
11
22
36
424
5120
💡

Why This Works

Step 1: Start with 1

We start the factorial result at 1 because multiplying by 1 does not change the value and it is the identity for multiplication.

Step 2: Multiply step by step

We multiply the result by each number from 1 up to n to accumulate the product that defines factorial.

Step 3: Handle zero case

If the input is 0, factorial is 1 by definition, so we return 1 immediately without looping.

🔄

Alternative Approaches

Recursive function
kotlin
fun factorial(n: Int): Long = if (n == 0) 1 else n * factorial(n - 1)

fun main() {
    val n = 5
    println("Factorial of $n is ${factorial(n)}")
}
This uses recursion to call the function itself until it reaches 0. It is elegant but can cause stack overflow for very large n.
Using Kotlin's fold function
kotlin
fun main() {
    val n = 5
    val factorial = (1..n).fold(1L) { acc, i -> acc * i }
    println("Factorial of $n is $factorial")
}
This uses Kotlin's functional style with fold to multiply all numbers. It is concise and readable.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs from 1 to n once, so the time grows linearly with n, making it O(n).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The iterative loop is fastest and safest for large n compared to recursion, which can cause stack overflow.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)Large inputs, safe and fast
Recursive functionO(n)O(n)Elegant code, small inputs
Fold functionO(n)O(1)Concise functional style
💡
Use a Long type variable to store factorial to handle larger results without overflow.
⚠️
Forgetting that factorial of 0 is 1, not 0, which causes wrong results.