Golang Program to Find the Product of Two Numbers Using Recursion


The product of two numbers is the result you get when you multiply them together. So 15 is the product of 3 and 5 and 22 is the product of 2 and 11 and so on.

A Recursion is where a function calls itself by direct or indirect means. Every recursive function has a base case or base condition which is the final executable statement in recursion and halts further calls.

Example-1: Golang Program Code to Find The Product of Two Numbers Using Recursion by Using The Direct Recursion Method

Syntax

Syntax for direct recursion
func recursion() {
   recursion()
}
func main() {
   recursion();
}

Algorithm

  • STEP 1 − Import the package fmt.

  • STEP 2 − Create the function product().

  • STEP 3 − We will use an if...else conditional statement.

  • STEP 4 − Start the function main().

  • STEP 5 − Initialize the integer variables.

  • STEP 6 − Call the function product().

  • STEP 7 − Print the result on the screen using fmt.Printf().

Example

package main  
// fmt package provides the function to print anything
import "fmt"  

func product(n int, m int) int {
   // if n less than m
   if n < m {
      return product(m, n)
   } else {
      // if m is not equal to 0      
      if (m != 0) {
         // recursive call to itself 
         return (n + product(n, m-1))
      } else {
         return 0
      }
   }
}
// Start the function main()
func main() {
  // Declare and initialize integer variables  
   var a int
   var b int
   var c int
   a = 4
   b = 15
   fmt.Println("The N1=",a,"\nThe N2 =",b)
   c = product(a, b)

   fmt.Printf("The product of two numbers: %d\n", c)
}

Output

The N1= 4 
The N2 = 15
The product of two numbers: 60

Description

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

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

  • Next we create a function product() to find the product of two numbers using the recursion technique.

  • We will use an if-else conditional statement which allows you to execute one block of code if the specified condition is true and another block of code if the condition is false.

  • If n is less than m, the function will return the product(m, n) and end the recursive function.

  • And if m is not equal to 0, then the function will recursive call to the function itself and return (n + product(n, m-1)).

  • Now start the function main ().

  • Next initialize the integer variables a and b which corresponds to the two numbers whose product has to be found. An integer c corresponds to the final result.

  • Now call the product () function

  • And print the result on the screen using fmt.Printf().

Example-2 to Find The Product of Two Numbers Using Recursion by Using Indirect Recursion Method

Syntax

Syntax for indirect recursion
func recursion_1() {
   recursion_2()}
func recursion_2(){
   recursion_1()}
func main() {
   recursion_1();
}

Algorithm

  • STEP 1 − Import the package fmt.

  • STEP 2 − Create the function product_1().

  • STEP 3 − We will use if...else conditional statement.

  • STEP 4 − Create the function product_2().

  • STEP 5 − Recursive call to the function product_1() indirectly.

  • STEP 5 − Start the function main().

  • STEP 6 − Initialize the integer variables x, y and z.

  • STEP 7 − Call the function product_1().

  • STEP 8 − Print the result on the screen using fmt.Printf().

Example

package main  
// fmt package provides the function to print anything
import "fmt"  

// defining the function with a parameter of int
// type and have a return type int
func product_1(n int, m int) int {
   
   // this is the base condition
    // if n less than m
   if n < m {
      return product_2(m, n)
   } else {
      // if m is not equal to 0      
      if (m != 0) {
         // recursive call to the second function product_2()
         return (n + product_2(n, m-1))
      } else {
         return 0
      }
   }
}
func product_2(n int, m int) int {
   
   // if n less than m
   if n < m {
      return product_1(m, n)
   } else {
      // if m is not equal to 0      
      if (m != 0) {
         // recursive call to the first function product_1() 
         return (n + product_1(n, m-1))
      } else {
         return 0
      }
   }
}
func main() {
  // Declare and initialize integer variable  
   var x int
   var y int
   var z int
   x = 11
   y = 15
   fmt.Println("The N1=",x,"\nThe N2 =",y)
   z = product_1(x, y)
 
   fmt.Printf("The product of two numbers: %d\n", z)
}

Output

The N1= 11 
The N2 = 15
The product of two numbers: 165

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.

  • Next we create a function product_1() to find the product of two numbers using the recursion technique.

  • We will use an if-else conditional statement which allows you to execute one block of code if the specified condition is true and another block of code if the condition is false.

  • If n is less than m, the function will return the product (m, n) and end the recursive function.

  • And if m is not equal to 0, then the function will recursively call to the second function product_2() indirectly and return (n + product(n, m-1)).

  • Next we create a function product_2 () and similarly to the above function make a recursive call to the function product_1() and return (n + product_1(n, m-1)).

  • Now start the function main().

  • Next initialize the integer variables x and y which correspond to the two numbers whose product has to be found. And integer z corresponds to the final result.

  • Now calling the first function product_1().

  • And finally print the result on the screen using fmt.Printf().

Updated on: 29-Dec-2022

177 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements