- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 decimal number to its binary form
Examples
- Input decimal_num = 13 => Output = 8+4+1 => 1101
- Input decimal_num = 11 => Output = 8+2+1 => 1011
Approach to solve this problem
- Step 1: Define a function that accepts a decimal number, decimal_num, type is int.
- Step 2: Define an array to store the remainder while dividing decimal number by 2.
- Step 3: Start a for loop until the decimal number becomes 0.
- Step 4: Print the binary array in reverse order.
Program
package main import ( "fmt" ) func decimalToBinary(num int){ var binary []int for num !=0 { binary = append(binary, num%2) num = num / 2 } if len(binary)==0{ fmt.Printf("%d\n", 0) } else { for i:=len(binary)-1; i>=0; i--{ fmt.Printf("%d", binary[i]) } fmt.Println() } } func main() { decimalToBinary(87) decimalToBinary(10) decimalToBinary(31) decimalToBinary(0) }
Output
1010111 1010 11111 0
- Related Articles
- Write a Golang program to convert a binary number to its decimal 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