Golang Program to convert Decimal to Octal


In this article you will know how to convert the decimal number to octal in Go language. For this we are going to use the FormatInt() function of strconv package.

When we say convert the decimal number to octal, it means to convert the number with the base value 10 to the base value 8. The base value means the number of digits required to represent the numeric value.

Example 1: Go Language Program to convert Decimal Number to Octal using Library Function

Syntax

func FormatInt(input int64, base int) string

for [condition | ( init; condition; increment) | Range] { 
   statement(s); 
}

func FormatInt(input int64, base int) string: FormatInt() function is used to convert integer values to another base values. This function takes two arguments one is the 64 bit integer value and another is the base value to which we want to convert the integer. This function then returns the final result as a string which we can store in a separate variable and print on the screen.

Algorithm

  • Step 1 − Import the package fmt and strconv.

  • Step 2 − Start the function main().

  • Step 3 − Declare and initialize the integer variables.

  • Step 4 − Call the FormatInt() function.

  • Step 5 − Print the result using the fmt package.

Example

//GO LANGUAGE PROGRAM TO CONVERT DECIMAL NUMBER TO OCTAL USING DIFFERENT FUNCTION package main // fmt package provides the function to print anything //strconv package provides the required function to convert from decimal to octal import ( "fmt" "strconv" ) // this is the main function func main() { //initialize the decimal value to a 64-bit int type variable naming num var num int64 num = 15 // storing the result of conversion in a seperate function called oct_num oct_num := strconv.FormatInt(num, 8) // printing the octal number fmt.Println("Decimal number", num, "is converted to octal as", oct_num) // taking another decimal number num = 34 // storing the result of conversion in a seperate function called oct_num oct_num = strconv.FormatInt(num, 8) // printing the octal number fmt.Println("Decimal number", num, "is converted to octal as", oct_num) //initialize the decimal value num = 26 // storing the result of conversion in a seperate function called oct_num oct_num = strconv.FormatInt(num, 8) // printing the octal number fmt.Println("Decimal number", num, "is converted to octal as", oct_num) }

Output

Decimal number 15 is converted to octal as 17
Decimal number 34 is converted to octal as 42
Decimal number 26 is converted to octal as 32

Description of the Code

  • First we have to import fmt package and the strconv package. fmt package allows us to print anything on the screen whereas strconv method contains the functions which we can use to convert a decimal number to octal number.

  • Then we need to initialize a 64 bit integer type variable to store the integer value that we wish to convert into octal.

  • In this example to convert integer to octal we will be using strconv.FormatInt() function.

  • Pass the integer value that you wish to convert as the first argument to the function and the base value as the second argument to the function.

  • Store the result in a separate variable. In this example we have used oct_num variable to store the result.

  • Then we can print the result on the screen using fmt.Println() function.

Example 2: Golang Program to Convert Decimal to Octal Using a Separate Function

Algorithm

  • Step 1 − Import the package fmt and strconv.

  • Step 2 − Initialize a toOctal() function that will contain the logic to convert a decimal value to octal.

  • Step 3 − Start the function main().

  • Step 4 − Declare and initialize the integer variables.

  • Step 5 − Call the toOctal() function by passing the integer value as argument to it.

  • Step 6 − Print the result using the fmt.Println() function.

Example

// GO LANGUAGE PROGRAM TO CONVERT DECIMAL NUMBER TO OCTAL USING FUNCTION package main import ( "fmt" ) // fmt package provides the function to print anything //strconv package provides the required function to convert from decimal to octal // initializing and defining a function to convert decimal to octal func toOctal(number int) int { // defining variables and assigning them values octal := 0 counter := 1 remainder := 0 for number != 0 { remainder = number % 8 number = number / 8 octal += remainder * counter counter *= 10 } return octal } // this is the main function func main() { var num int //initialize the decimal value num = 154 // calling the toOctal() function and storing its result in oct_num() function oct_num := toOctal(num) // printing the octal number fmt.Println("Decimal number", num, "is converted to octal as", oct_num) }

Output

Decimal number 154 is converted to octal as 232

Description of the Code

  • First we have to import fmt package that allows us to print anything on the screen.

  • Then we need to initialize integer type variable to store the integer value that we wish to convert

  • Call the toOctal() function by passing the integer value as argument to it.

  • In this function we are using for loop as a while loop to store the particular octal representation.

  • First we need to store the remainder by dividing the number with 8 then we need to skip the last digit and repeat the process for the remaining number of digits.

  • Then we need to store the result in the octal variable. To store the next digit in hundreds place and so on we need to multiply the counter variable with 10.

  • Return the result and print it on screen.

Conclusion

We have successfully compiled and executed a golang program to convert decimal to octal using 2 different techniques shown above.

Updated on: 14-Nov-2022

443 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements