Golang Example - Reverse of a number or integer


In this program, You will see a three program 

First is to reverse of a number or integer, the second program is the reverse number entered by user , thrid is reverse number using a recursive function in Go Language.

To understanding the below programs, You have following features understanding 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 new integer ie reverse of a number to console

Example program to reverse of a number

In this program, Golang code is to reverse of 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 is fixed, In this program, the program read input from a user via the console.

Following is a program to reverse of 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

Recursive function is called to reverse a number. Recursive function is called inside a 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  

Similar Posts