How to Reverse of a number or integer in Golang Code example

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

How 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

How to to reverse a number entered by user input in Golang?

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

How to reverse a number using recursive function in Golang?

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:

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.