Write a kotlin Program to find Factorial of a Given Number

Write a program to find factorial of a given number is a common kotlin program asked in any interview to freshers.

The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.

For example factorial of 4 is 4*3*2 = 24

There are 2 ways to find a factorial of a given number - One by using for loop and the other using recursion.

Following kotlin program uses for loop to find factorial of a given number.

fun findFactorial(num: Int): Long {
    if (num < 1) {
        println("Please provide non-negative number.")
    var factorial: Long = 1
    for (i in num downTo 2) {
        factorial = factorial * i
    return factorial

fun main(args: Array<String>) {
    val num = 5
    println("Factorial of " + num + " is : " + findFactorial(num))


Factorial of 5 is : 120

Use for loop starting from the number itself and multiply with a decrement number and keeps on accumulating the result in the same variable until 1 is reached.

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions