- 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
Golang Program to convert an integer into binary representation
Examples
For example, n = 1 (Binary Representation of 1: 1)
For example, n = 5 (Binary Representation of 5: 101)
For example, n = 20 (Binary Representation of 5: 10100)
For example, n = 31 (Binary Representation of 31: 11111)
Approach to solve this problem
Step 1 − Define a method that accepts an integer, n.
Step 2 − Convert n into binary representation using golang package
Step 3 − Return the converted binary representation.
Example
package main import ( "fmt" "strconv" ) func IntegerToBinary(n int) string { return strconv.FormatInt(int64(n), 2) } func main(){ n := 1 fmt.Printf("Binary Representation of %d is %s.\n", n, IntegerToBinary(n)) n = 5 fmt.Printf("Binary Representation of %d is %s.\n", n, IntegerToBinary(n)) n = 20 fmt.Printf("Binary Representation of %d is %s.\n", n, IntegerToBinary(n)) n = 31 fmt.Printf("Binary Representation of %d is %s.\n", n, IntegerToBinary(n)) }
Output
Binary Representation of 1 is 1. Binary Representation of 5 is 101. Binary Representation of 20 is 10100. Binary Representation of 31 is 11111.
- Related Articles
- Java Program to convert an integer into binary
- Haskell Program to convert the string into an integer
- C++ Program to convert the string into an integer
- Golang Program to convert Binary to Octal
- C# program to convert binary string to Integer
- Golang program to convert array into slice
- Golang program to convert slice into array
- Golang Program to convert boolean variables into string
- Golang Program to convert a string into Uppercase
- Golang Program to convert a string into lowercase
- Golang program to convert a linked list into an array and vice-versa
- Golang Program to Convert Centimeters into Feet and Inches
- Golang Program to convert string type variables into Boolean
- Golang Program to convert string type variables into int
- Golang program to convert the hash collection into string

Advertisements