{

# Golang Example - Convert(cast) Octal to Decimal Number In this blog post, You will learn two programs in the Go language.

• The first program is to convert Octal to Decimal Number
• The second program is to convert Decimal to Octal Number.

An Octal number is a number based on the base 8 number system. It contains digits from 0 to 7. In Golang, Octal numbers are always prefixed with zero. Example octal number is 0879. Decimal Numbers are numbers that contain numbers based on base 10.

## Example Program to convert Octal to Decimal Number

We have 2 ways to Convert Octal to Decimal Number in Go language. The following are the ways.

• Using strconv ParseInt function
• Custom Function using for loop without inbuilt functions

The below program takes Octal numbers (prefixed with zero or the normal number for Declaring Octal numbers) and stored in variable octal.

### strconv ParseInt function example to cast Octal to Decimal

`strconv` package provides `ParseInt` function. To convert Octal number, `ParseInt` is provided with number and base=8.

Here is an example program for conversion to Decimal Number

``````package main

import (
"fmt"
"strconv"
)

func main() {
var octal string
fmt.Print("Enter Octal Number:")
fmt.Scanln(&octal)
output, err := strconv.ParseInt(octal, 8, 64)
if err != nil {
fmt.Println(err)
return
}

fmt.Printf("Output %d", output)
}

``````

When the above program is compiled, Output is

``````Enter Octal Number:045
Output 37

``````

### Custom Function without Predefined functions

Created a user-defined function, inside a custom function, used for loop with Modulus, Division operator, and Math pow function.

Here is an example program

``````package main

import (
"fmt"
"math"
)

func convertOctaToDecimal(number int) int {
decimal := 0
counter := 0.0
remainder := 0

for number != 0 {
remainder = number % 10
decimal += remainder * int(math.Pow(8.0, counter))
number = number / 10
counter++
}
return decimal
}

func main() {
var octal int
fmt.Print("Enter Octal Number:")
fmt.Scanln(&octal)

fmt.Printf("Output %d", convertOctaToDecimal(octal))

}

``````

Output:

``````Enter Octal Number:25
Output 21
``````

### Example Program to convert Decimal to Octal Number

We have 2 ways to convert Decimal to Octal numbers in Golang.
Below are two ways

• using strconv FormatInt function
• Without Inbuilt Function

The below program takes decimal input numbers from the keyboard console, stores them in variable decimal.

### strconv FormatInt function example to convert Decimal to Octal

Inbuilt Standard package `strconv` provides the `FormatInt` function used to convert Decimal to Octal number.
Here is an example program for Casting Decimal to Octal

``````package main

import (
"fmt"
"math"
"strconv"
)
func main() {
var decimal int64
fmt.Print("Enter Decimal Number:")
fmt.Scanln(&decimal)
output := strconv.FormatInt(decimal, 8)
fmt.Print("Output ", output)

}

``````

The output of the compiled program is

``````Enter Decimal Number:124
Output 174

``````

### Custom function without inbuilt functionality

The following program uses Golang features For loop, Modulus, and division operators.
Here is an example program to do the conversion.

``````package main

import (
"fmt"
)

func convertDecimalToOctal(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() {
var decimal int
fmt.Print("Enter Decimal Number:")
fmt.Scanln(&decimal)
fmt.Print("Output ", convertDecimalToOctal(decimal))

}

``````

Output:

``````Enter Decimal Number:145
Output 221
``````
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.

Related posts