How to find the Reverse of a given number using Recursion in Golang?


In this tutorial, we are going to learn how we can find the reverse of the given number in the Golang programming language. The recursive function is more suitable if we want to modify the function that is not possible with a For loop. In this article, we are going to achieve this by using recursion.

Explanation

Iteration 1:

Number = 54678
Reverse of number = 0
Reverse of number = Reverse of number * 10 + Number % 10
   = 0 + 54678 % 10
   = 0 + 8
   = 8
Number = Number / 10
   = 54678 / 10
   = 5467

Iteration 2:

Number = 5467
Reverse of number = 8
Reverse of number = Reverse of number * 10 + Number % 10
   = 8 * 10 + 5467 % 10
   = 80 + 7
   = 87
Number = Number / 10
   = 5467 / 10
   = 546

Iteration 3:

Number = 546
Reverse of number = 87
Reverse of number = Reverse of number * 10 + Number % 10
   = 87 * 10 + 546 % 10
   = 870 + 6
   = 876
Number = Number / 10
   = 546 / 10
   = 54

Iteration 4:

Number = 54
Reverse of number = 876
Reverse of number = Reverse of number * 10 + Number % 10
   = 876 * 10 + 54 % 10
   = 8760 + 4
   = 8764
Number = Number / 10
   = 54 / 10
   = 5

Iteration 5:

Number = 5
Reverse of number = 8764
Reverse of number = Reverse of number * 10 + Number % 10
   = 8764 * 10 + 5 % 10
   = 87640 + 5
   = 87645
Number = Number / 10
   = 5 / 10
   = 0

Finding the Reverse of a Number using Recursion

In this example, we are going to find the reverse using recursion. We are storing the reverse of the number in a global variable.

Algorithm

  • Step 1 − Declare the variables to store the actual number and the reverse of the number.

  • Step 2 − Call the recursive function with the number as an argument.

  • Step 3 − Printing the result.

Example

package main
import (

   // fmt package provides the function to print anything
   "fmt"
)

// declaring the variable to store the reverse of the number
var reverseOfNumberGlobal = 0
func reverse(number int) int {

   // returning if the number becomes zero
   if number == 0 {
      return 0
   }

   // finding reverse using mod and divide operator
   reverseOfNumberGlobal = reverseOfNumberGlobal*10 + (number % 10)

   // calling the function with a number as an argument
   reverse(number / 10)

   // returning the reverse
   return reverseOfNumberGlobal
}
func main() {

   // declaring the variable
   var number, reverseOfNumber int

   // initializing the variable
   number = 54678
   fmt.Println("Golang program to find the reverse of a number using a recursive function.")
   fmt.Printf("The number is %d. \n", number)

   // running for loop till the number becomes zero
   reverseOfNumber = reverse(number)

   // printing the result
   fmt.Printf("The reverse is %d. \n", reverseOfNumber)
}

Output

Golang program to find the reverse of a number using a recursive function.
The number is 54678.
The reverse is 87645.

Conclusion

This is how we can find the reverse of a number using recursion in Golang. The number of recursion calling is equal to the number of digits. To learn more about go you can explore these tutorials.

Updated on: 11-Jan-2023

430 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements