{

Golang Example - Reverse of a number or integer


In this program, You will see three programs.

The first is to reverse a number or integer, the second program is the reverse number entered by the user, third is the reverse number using a recursive function in Go Language.

To understand the below programs, You have the understanding of the following features in Go Language.

Following is a sequence of steps.

  • Give an input number, First, convert an integer to String using strconv Itoa() function
  • Iterate each number character of a string using for loop, append each number to a new string
  • Convert the new string to an integer using strconv Atoi() function
  • finally, print a new integer i.e reverse of a number to console

Example program to reverse a number in a Golang

In this program, Golang code is to reverse a given number.

package main  
  
import (  
 "fmt"  
 "strconv"  
)  
  
func ReverseNumber(number int) int {  
 strNumber := strconv.Itoa(number)  
 reverseStrNumber := ""  
 for length := len(strNumber); length > 0; length-- {  
  reverseStrNumber += string(strNumber[length-1])  
 }  
 reverseNum, error := strconv.Atoi(reverseStrNumber)  
 if error != nil {  
  fmt.Println("Failure to cast String to int")  
 }  
 return reverseNum  
}  
  
func main() {  
 fmt.Printf("Reverse of number(%d) is %d\n", 1213, ReverseNumber(1213))  
 fmt.Printf("Reverse of number(%d) is %d\n", 5874, ReverseNumber(5874))  
  
}  

When you compiled and executed the above program, the output is

Reverse of number(1213) is 3121  
Reverse of number(5874) is 4785  

Example program 2 to reverse a number entered by user input

Instead of input being fixed, In this program, the program reads input from a user via the console.

Following is a program to reverse a number read number from keyboard console.

package main  
  
import (  
 "fmt"  
 "strconv"  
)  
  
func ReverseNumber(number int) int {  
 strNumber := strconv.Itoa(number)  
 reverseStrNumber := ""  
 for length := len(strNumber); length > 0; length-- {  
  reverseStrNumber += string(strNumber[length-1])  
 }  
 reverseNum, error := strconv.Atoi(reverseStrNumber)  
 if error != nil {  
  fmt.Println("Failure to cast String to int")  
 }  
 return reverseNum  
}  
  
func main() {  
 var number int  
 fmt.Print("Enter number to reverse ")  
 fmt.Scanln(&number)  
 fmt.Printf("Reverse of number(%d) is %d\n", number, ReverseNumber(number))  
  
}  

When you compiled and executed the above program, the output is

Enter number to reverse 25  
Reverse of number(25) is 52  
  
Enter number to reverse 89754  
Reverse of number(89754) is 45798  

Example program 3- recursive function to reverse number

The recursive function is called to reverse a number. A recursive function is called inside the same function.

package main  
import (  
 "fmt"  
)  
  
func reverse(number int, outputNumber int) int {  
 if number != 0 {  
  outputNumber = outputNumber * 10  
  outputNumber = outputNumber + number%10  
  number = number / 10  
 } else {  
  return outputNumber  
 }  
 return reverse(number, outputNumber)  
  
}  
  
func main() {  
 var number int  
 fmt.Print("Enter number to reverse ")  
 fmt.Scanln(&number)  
 fmt.Printf("Reverse of number(%d) is %d\n", number, reverse(number, 0))  
  
}  

Output is

Enter number to reverse 98745  
Reverse of number(98745) is 54789  
  
Enter number to reverse 2541  
Reverse of number(2541) is 1452  

Conclusion

In this short tutorial, You learned different methods to reverse a number by taking input from the console and using recursive function.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.