- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Haskell Program to find the reverse of a given number using recursion
- Swift program to find the reverse of a given number using recursion
- Write a Golang program to find the factorial of a given number (Using Recursion)
- Java Program to Find Reverse of a Number Using Recursion
- Golang Program to Reverse a Sentence using Recursion
- How to find the LCM of two given numbers using Recursion in Golang?
- How to find the GCD of Two given numbers using Recursion in Golang?
- Java program to find the factorial of a given number using recursion
- How to Find the Power of a Number Using Recursion in Python?
- How to Find Factorial of Number Using Recursion in Python?
- How to find the sum of digits of a number using recursion in C#?
- Golang Program to Find G.C.D Using Recursion
- Java program to calculate the power of a Given number using recursion
- Java program to calculate the GCD of a given number using recursion
- Golang Program to Find the Product of Two Numbers Using Recursion
