Created
October 1, 2016 02:04
-
-
Save husobee/682dee04d2400261f69318eab14656bf to your computer and use it in GitHub Desktop.
reverse an int (friend's interview question)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
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
432432301