Golang Program to Find G.C.D Using Recursion


In this tutorial we will discuss how to write a Golang program to find the greatest common divisor GCD using recursion.

The greatest common divisor (GCD) of two or more numbers is the greatest common factor number that divides them, exactly. It is also called the highest common factor (HCF). For example, the greatest common factor of 15 and 10 is 5, since both the numbers can be divided by 5.

15/5 = 3
10/5 = 2

Algorithm

  • Step 1 − Import the package fmt

  • Step 2 − Start function main()

  • Step 3 − We will use an if-conditional statement

  • Step 4 − Recursive call to the function itself

  • Step 5 − Start the function main()

  • Step 6 − Call the function factorialnumber ()

  • Step 7 − Print the result on the screen using fmt.Println()

Example 1

// GOLANG PROGRAM TO FIND GCD USING RECURSION package main // fmt package provides the function to print anything import "fmt" // function prototype func main() { // declare the variables var n1 int var n2 int fmt.Println("Golang Program to find GCD using recursion") // initialize the variables n1 = 36 n2 = 60 // print the result using in-built function fmt.Println() fmt.Println("G.C.D OF", n1, n2,"is",hcf(n1,n2)) } // create the function hcf() func hcf(n1 int, n2 int) int { if (n2 != 0) { return hcf(n2, n1 % n2); } else { return n1; } }

Output

Golang Program to find GCD using recursion
G.C.D OF 36 60 is 12

Description of the Code

  • In the above program, we first declare the package main

  • We imported the fmt package that includes the files of package fmt

  • Now start the function main().GO program execution starts with the function main()

  • Next we declare and initialize the integer variables n1 and n2

  • Now create the function hcf()

  • We will use if-else conditional statements to execute the code

  • In the line of code : return hcf(n2, n1 % n2) : here the function calls itself, recursive call

  • And finally printing the result on the screen using in-built function fmt.Println().This function is defined under the fmt package and it helps to write standard output.

Algorithm

  • Step 1 − Import the package fmt

  • Step 2 − Start function main()

  • Step 3 − Declare and initialize the variables

  • Step 4 − Create the function gcd()

  • Step 5 − We will use if else conditional statement

  • Step 6 − Recursive call to the function itself

  • Step 7 − Print the result using fmt.Printf().

Example 2

// GOLANG PROGRAM TO FIND GCD USING RECURSION package main // fmt package provides the function to print anything import "fmt" func main() { // declare the variables var a int var b int var result int fmt.Println("Golang Program to find GCD using recursion") // initialize the variables a = 300 b = 60 result = gcd(a,b) // print the result using in-built function fmt.Printf() fmt.Printf("The GCD of %d and %d is %d.\n",a,b,result) } // create the function gcd() func gcd(a int,b int) int { for a != b { if b < a { return gcd(a - b,b) } else { return gcd(a,b - a) } } return a }

Output

Golang Program to find GCD using recursion
The GCD of 300 and 60 is 60.

Description of the Code

  • In the above program, we first declare the package main

  • We imported the fmt package that includes the files of package fmt

  • Now start the function main().GO program execution starts with the function main()

  • Next we declare and initialize the integer variables a, b and result

  • Variables ‘a’ and ‘b’ correspond to the integers whose GCD is to be found. Variable result corresponds to the final result after calculating the GCD.

  • Now create the function gcd()

  • We will use for loop and if-else conditional statements to execute the code. For loop is used to check that both the ‘a’ and ‘b’ variable values are not equal. If this condition is true then the loop is executed. If condition is false return the value of ‘a’. If-else condition statement is used to check the value of ‘a’ variable is greater than the value of ‘b’ variable. If the condition is true, then return two integer variable values. Otherwise, if the condition is false, then execute else statement and return the values of two integer variable.

  • In the lines of code: return gcd(a - b,b): return gcd(a,b - a) : here the function calls itself, recursive call

  • And finally printing the result on the screen using in-built function fmt.Printf().This function is defined under the fmt package and it helps to write standard output.

Conclusion

In the above two examples we have successfully compiled and executed the Golang program code to find the greatest common divisor GCD using recursion technique.

We are using fmt println() and fmt.Println() functions for printing the result on the output screen. Here we are showing how to implement conditional statement in the Go language. We have used if condition in one example and for loop in second example.

Updated on: 25-Oct-2022

792 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements