Skip to content

Instantly share code, notes, and snippets.

@alanmsmxyz
Created September 6, 2019 07:56
Show Gist options
  • Save alanmsmxyz/a151826bda252b2b8dc68cc4cab04008 to your computer and use it in GitHub Desktop.
Save alanmsmxyz/a151826bda252b2b8dc68cc4cab04008 to your computer and use it in GitHub Desktop.
Simple algorithm to convert decimal to binary in golang
/*
* 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