Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "os"
- "strconv"
- "strings"
- "fmt"
- )
- func decToBin(n int64) string {
- var bin string
- for n > 0 {
- if n%2 == 1 {
- n--
- n /= 2
- bin = "1" + bin
- } else {
- n /= 2
- bin = "0" + bin
- }
- }
- return bin
- }
- func main() {
- reader:= bufio.NewReader(os.Stdin)
- fmt.Printf("Enter a decimal nuber\n")
- input, _ := reader.ReadString('\n')
- dec, _ := strconv.ParseInt(strings.TrimSpace(input),10, 64)
- fmt.Printf("The binary number is: %s\n",decToBin(dec))
- }
Add Comment
Please, Sign In to add comment