Menu Close

Go – Program to convert decimal number to binary in go

Here, we will see program to convert decimal number to binary in go. Given below, you can find algorithm and program.

To convert decimal number to binary number, we will use FormatInt() function of strconv package.

Input: num = 10
Binary number = 1010

Input: 4
Binary number = 100

Input: 15
Binary number = 1111

Code:

package main

import (

  "fmt"
  "strconv"
)

func main() {

  var num int64

  num = 10

  oct_num := strconv.FormatInt(num, 2)

  fmt.Println("binary num: ", oct_num)


  num = 4

  oct_num = strconv.FormatInt(num, 2)

  fmt.Println("binary num: ", oct_num)


   num = 15

  oct_num = strconv.FormatInt(num, 2)

  fmt.Println("binary num: ", oct_num)

}

Output:

binary num: 1010

binary num: 100

binary num: 1111

To learn more about golang. Please follow given below link.

https://www.techieindoor.com/go-lang/

References:

https://golang.org/doc/
https://golang.org/pkg/

Posted in golang, golang program

Leave a Reply

Your email address will not be published. Required fields are marked *