- 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 Binary to Octal
In this article you will learn the go language code to convert binary number to octal number. Binary numbers are the numbers that have base 2, i.e., each digit can have only 2 possibilities 0 and 1. Octal numbers are the numbers that have base 8, i.e., each digit can have only 8 possibilities from 0 to 7.
Algorithm
STEP 1 − Import the fmt package.
STEP 2 − Start the main function and initialize the required variables of type int naming binary, octal, remainder and j. store initial values to them.
STEP 3 − Store the binary number chosen to a different variable to be used later and start the for loop. We will be using this for loop as a while loop.
STEP 4 − In this loop, we need to first find the remainder of the binary number by dividing it by 10.
STEP 5 − Then find the octal number by multiplying remainder with j and then adding the previous octal value to it. Increase the count of j by a multiple of 2.
STEP 6 − Print the result on the screen using fmt.Println() function.
STEP 7 − Repeat the above steps with different binary numbers and print their octal equivalents on the screen.
Example 1
In this example we will write a go language program to convert binary numbers to octal using mod function
package main import "fmt" func main() { var binary int var octal int = 0 j := 1 var remainder int binary = 10011011 binarynumber := binary for binary != 0 { remainder = binary % 10 octal = octal + remainder*j j = j * 2 binary = binary / 10 } fmt.Println("The octal representation of binary number ", binarynumber, "is", octal) binary = 111111 binarynumber = binary for binary != 0 { remainder = binary % 10 octal = octal + remainder*j j = j * 2 binary = binary / 10 } fmt.Println("The octal representation of binary number ", binarynumber, "is", octal) }
Output
The octal representation of binary number 10011011 is 155 The octal representation of binary number 111111 is 16283
Example 2
In this program we will write a go language code to convert a binary number to octal. To achieve this result we will first convert the binary number to decimal then convert the decimal equivalent to octal.
package main import ( "fmt" "math" ) // initializing and defining a binary number to convert it into decimal func toDecimal(number int) int { var remainder int value := 0 decimalNumber := 0 for number != 0 { remainder = number % 10 number = number / 10 decimalNumber = decimalNumber + remainder*int(math.Pow(2, float64(value))) value++ } return decimalNumber } // initializing and defining a function to convert decimal to octal func toOctal(number int) int { octal := 0 counter := 1 remainder := 0 for number != 0 { remainder = number % 8 number = number / 8 octal += remainder * counter counter *= 10 } return octal } func main() { number := 1001 binarynum := number result := toDecimal(number) oct_num := toOctal(result) fmt.Println("The octal representation of binary number =", binarynum, "is: ", oct_num) number = 1100111 binarynum = number result = toDecimal(number) oct_num = toOctal(result) fmt.Println("The octal representation of binary number =", binarynum, "is: ", oct_num) }
Output
The octal representation of binary number = 1001 is: 11 The octal representation of binary number = 1100111 is: 147
Conclusion
We have successfully compiled and executed a golang program to convert a binary number to octal along with examples. In the first program, we are using for loops to convert binary number to octal while in the second one we have created two functions one to convert binary to decimal and other to convert the obtained decimal result to octal.
- Related Articles
- Haskell Program to convert Binary to Octal
- JAVA Program to Convert Binary to Octal
- JAVA Program to Convert Octal to Binary
- Golang Program to convert Decimal to Octal
- C++ Program to Convert Octal Number to Binary Number
- How to Convert Binary to Octal?
- How to Convert Octal to Binary?\n
- C++ Program to Convert Binary Number to Octal and vice-versa
- Java Program to convert integer to octal
- Java Program to convert Decimal to Octal
- JAVA Program to Convert Octal to Hexadecimal
- Swift Program to convert Decimal to Octal
- Haskell Program to convert Decimal to Octal
- Golang Program to convert an integer into binary representation
- How to Convert Decimal to Binary, Octal, and Hexadecimal using Python?
