Kotlin example for Beginners – Kotlin Program to Find GCD of two Numbers

Kotlin Program to Find GCD of two Numbers

In this program, you’ll learn to find GCD of two numbers in Kotlin. This is done by using while loop with the help of if else statement.

The HCF or GCD of two integers is the largest integer that can exactly divide both numbers (without a remainder).

Example 1: Find GCD of two numbers using while loop


fun main(args: Array<String>) {
    val n1 = 81
    val n2 = 153
    var gcd = 1

    var i = 1
    while (i <= n1 && i <= n2) {
        // Checks if i is factor of both integers
        if (n1 % i == 0 && n2 % i == 0)
            gcd = i
        ++i
    }

    println("G.C.D of $n1 and $n2 is $gcd")
}

When you run the program, the output will be:

G.C.D of 81 and 153 is 9

Here, two numbers whose GCD are to be found are stored in n1 and n2 respectively.

Then, a while loop is executed until i is less than both n1 and n2. This way, all numbers between 1 and smallest of the two numbers are iterated to find the GCD.

If both n1 and n2 are divisble by igcd is set to the number. This goes on until it finds the largest number (GCD) which divides both n1 and n2 without remainder.

 


There is a better alternative for finding GCD in Kotlin as follows:

Example 2: Find GCD of two numbers (Better Alternative)


fun main(args: Array<String>) {
    var n1 = 81
    var n2 = 153

    while (n1 != n2) {
        if (n1 > n2)
            n1 -= n2
        else
            n2 -= n1
    }

    println("G.C.D = $n1")
}

When you run the program, the output will be:

G.C.D = 9

This is a better way to find the GCD. In this method, smaller integer is subtracted from the larger integer, and the result is assigned to the variable holding larger integer. This process is continued until n1 and n2 are equal.

The above two programs works as intended only if the user enters positive integers. Here’s a little modification of the second example to find the GCD for both positive and negative integers.

 


Example 3: GCD for both positive and negative numbers


fun main(args: Array<String>) {
    var n1 = 81
    var n2 = -153

    // Always set to positive
    n1 = if (n1 > 0) n1 else -n1
    n2 = if (n2 > 0) n2 else -n2

    while (n1 != n2) {
        if (n1 > n2)
            n1 -= n2
        else
            n2 -= n1
    }

    println("G.C.D = $n1")
}

When you run the program, the output will be:

G.C.D = 9

 

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.