- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Golang program to convert a binary number to its decimal form
Examples
- Input binary_num = 1010111 => decimal number = 64+0+16+4+2+1 = 87
- Input binary_num = 10101 => decimal number = 21
Approach to solve this problem
- Step 1: Define a function that accepts a binary number, binary_num, declare decimalNum = 0, index = 0
- Step 2: Start for loop until binary_num becomes 0.
- Step 3: Find the remainder of binary_num and divide by 10.
- Step 4: Calculate the decimal number using decimalNum and remainder*pow(2, index).
- Step 5: Return decimalNum.
Program
package main import ( "fmt" "math" ) func binaryToDecimal(num int) int { var remainder int index := 0 decimalNum := 0 for num != 0{ remainder = num % 10 num = num / 10 decimalNum = decimalNum + remainder * int(math.Pow(2, float64(index))) index++ } return decimalNum } func main(){ fmt.Println(binaryToDecimal(1)) fmt.Println(binaryToDecimal(10)) fmt.Println(binaryToDecimal(101)) fmt.Println(binaryToDecimal(101011)) }
Output
1 2 5 43
- Related Articles
- Write a Golang program to convert a decimal number to its binary form
- Haskell program to convert a decimal number into a binary number
- Java Program to convert binary number to decimal number
- C++ program to Convert a Decimal Number to Binary Number using Stacks
- C++ Program To Convert Decimal Number to Binary
- Python program to convert decimal to binary number
- How to convert a decimal number to a fraction reduced to its lowest form?
- Convert decimal to binary number in Python program
- Java program to convert decimal number to binary value
- Java program to convert binary number to decimal value
- Golang Program to convert Decimal to Hexadecimal
- Golang Program to convert Decimal to Octal
- Golang Program to convert Hexadecimal to Decimal
- C++ Program to Convert Binary Number to Decimal and vice-versa
- Swift program to convert the decimal number to binary using recursion

Advertisements