# Golang Example - Convert(cast) Octal to Decimal Number

In this blog post, You will learn two programs in Go language
The first program is to convert Octal to Decimal Number
Second program 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 numbers is 0879. Decimal Number are numbers which 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. Following are the ways.
• Using strconv ParseInt function
• Custom Function using for loop without inbuilt functions
The below program takes Octal number (prefixed with zero or 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 an 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 is
``````Enter Octal Number:25
Output 21
``````

### Example Program to convert Decimal to Octal Number

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

• using strconv FormatInt function
• Without Inbuilt Function

Below program takes decimal input number from keyboard console, stores it in variable decimal.

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

Inbuilt Standard package strconv provides 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 is
``````Enter Decimal Number:145
Output 221
``````