Kotlin example for Beginners – Kotlin Program to Find Factorial of a Number

Kotlin Program to Find Factorial of a Number

In this program, you’ll learn to find the factorial of a number using for and while loop in Kotlin. You’ll also learn to use ranges to solve this problem.

The factorial of a positive number n is given by:

factorial of n (n!) = 1 * 2 * 3 * 4 * ... * n

Example 1: Find Factorial of a number using for loop


fun main(args: Array<String>) {

    val num = 10
    var factorial: Long = 1
    for (i in 1..num) {
        // factorial = factorial * i;
        factorial *= i.toLong()
    }
    println("Factorial of $num = $factorial")
}

When you run the program, the output will be:

Factorial of 10 = 3628800

In this program, we’ve used for loop to loop through all numbers between 1 and the given number num (10), and the product of each number till num is stored in a variable factorial.

Unlike Java, in Kotlin, you can use ranges (1..num) and in operator to loop through numbers between 1 to num.

Also, we’ve used long instead of int to store large results of factorial.

However, it’s still not big enough to store the value of bigger numbers (say 100). For results that cannot be stored in a long variable, we use BigInteger variable declared in java.math library.

 


Example 2: Find Factorial of a number using BigInteger


import java.math.BigInteger

fun main(args: Array<String>) {

    val num = 30
    var factorial = BigInteger.ONE
    for (i in 1..num) {
        // factorial = factorial * i;
        factorial = factorial.multiply(BigInteger.valueOf(num.toLong()))
    }
    println("Factorial of $num = $factorial")
}

When you run the program, the output will be:

Factorial of 30 = 205891132094649000000000000000000000000000000

Here, instead of long, we use BigInteger variable factorial.

Since, * cannot be used with BigInteger, we instead use multiply() for the product. Also, num should be casted to BigInteger for multiplication.


Likewise, we can also use a while loop to solve this problem.

Example 3: Find Factorial of a number using while loop


fun main(args: Array<String>) {

    val num = 5
    var i = 1
    var factorial: Long = 1
    while (i <= num) {
        factorial *= i.toLong()
        i++
    }
    println("Factorial of $num = $factorial")
}

When you run the program, the output will be:

Factorial of 5 = 120

In the above program, unlike a for loop, we have to increment the value of i inside the body of the loop.

Though both programs are technically correct, it is better to use for loop in this case. It’s because the number of iteration (upto num) is known.

Visit this page to learn to find factorial of a number using recursion.

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.