Menu Close

Go – Program to convert binary number to decimal in go

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

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

Input: binary num = 1010
Decimal number = 10

Input: binary num = 111
Decimal number = 7

Input: binary num = 1111
Decimal number = 15

Code:

package main

import (

  "fmt"
  "strconv"
)

func main() {

  bin_num := "1010"

  num, err := strconv.ParseInt(bin_num, 2, 64)

  if err != nil {

    panic(err)

  }

  fmt.Println("decimal num: ", num)


  bin_num = "111"

  num, err = strconv.ParseInt(bin_num, 2, 64)

  if err != nil {

    panic(err)

  }

  fmt.Println("decimal num: ", num)


  bin_num = "1111"

  num, err = strconv.ParseInt(bin_num, 2, 64)

  if err != nil {

    panic(err)

  }

  fmt.Println("decimal num: ", num)

}

Output:

decimal num: 10

decimal num: 7

decimal num: 15

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 *