Scala program to find GCD of two numbers

1. Introduction

The Greatest Common Divisor (GCD) of two numbers is the largest number that can divide both of them without leaving a remainder. It's a fundamental concept in number theory and has applications in areas like cryptography. In this blog post, we'll create a Scala program to determine the GCD of two provided numbers.

2. Program Steps

1. Set up the Scala environment.

2. Create an object to house the main function.

3. Define the two numbers for which the GCD needs to be found.

4. Implement a recursive function to determine the GCD.

5. Call the GCD function and display the result.

6. Run the program.

3. Code Program

object GCDFinderApp {
  def main(args: Array[String]): Unit = {
    // Define the two numbers
    val num1 = 56
    val num2 = 98

    println(s"Finding the GCD of numbers: $num1 and $num2")

    // Calculate and print the GCD using the gcd function
    val result = gcd(num1, num2)
    println(s"\nThe GCD of $num1 and $num2 is: $result")

  // Recursive function to determine the GCD
  def gcd(a: Int, b: Int): Int = {
    if(b == 0) a else gcd(b, a % b)


Finding the GCD of numbers: 56 and 98
The GCD of 56 and 98 is: 14


1. We start with an object named GCDFinderApp. In Scala, the main method, serving as the program's entry point, is situated within an object.

2. Inside the main function, we define two values, num1 and num2, representing the numbers for which we want to find the GCD.

3. We notify the user of the numbers being processed.

4. The GCD is determined using the Euclidean algorithm, which is a method to find the GCD of two numbers a and b by recursively applying the formula gcd(a, b) = gcd(b, a % b) until b becomes zero.

5. In the main function, we invoke the gcd function to obtain the GCD and subsequently display the result.

6. As observed in the output, the program first denotes the numbers being processed and then shows the computed GCD.