Created
September 6, 2019 07:56
-
-
Save alanmsmxyz/a151826bda252b2b8dc68cc4cab04008 to your computer and use it in GitHub Desktop.
Simple algorithm to convert decimal to binary in golang
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
/* | |
* decimalToBinary.go | |
* Simple algorithm to convert decimal to binary in golang | |
* | |
* 2019 - Mochamad Syarief Maulana | |
*/ | |
package main | |
import "fmt" | |
var n, input int | |
func main() { | |
fmt.Scanln(&input) | |
n = 1 | |
for n*2 <= input { | |
n = n * 2 | |
} | |
for n >= 1 { | |
fmt.Print(input / n) | |
input = input % n | |
n = n / 2 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment