Skip to content

Instantly share code, notes, and snippets.

@husobee
Created October 1, 2016 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save husobee/682dee04d2400261f69318eab14656bf to your computer and use it in GitHub Desktop.
Save husobee/682dee04d2400261f69318eab14656bf to your computer and use it in GitHub Desktop.
reverse an int (friend's interview question)
package main
import (
"fmt"
)
func reverse(input int) int {
var result int
for ; input != 0; input = input / 10 {
result = input%10 + 10*result
}
return result
}
func main() {
fmt.Println(reverse(103234234))
}
@husobee
Copy link
Author

husobee commented Oct 1, 2016

432432301

@husobee
Copy link
Author

husobee commented Oct 1, 2016

since decimal numbers are in base10, mod 10 will give you the "ones" column, and create a new decimal number by dividing the original value by 10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment