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

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">//GO LANGUAGE PROGRAM TO CONVERT DECIMAL NUMBER TO OCTAL USING DIFFERENT FUNCTION</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token comment">//strconv package provides the required function to convert from decimal to octal</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"strconv"</span>
<span class="token punctuation">)</span>

<span class="token comment">// this is the main function</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">//initialize the decimal value to a 64-bit int type variable naming num</span>
   <span class="token keyword">var</span> num int64
   num <span class="token operator">=</span> <span class="token number">15</span>
   
   <span class="token comment">// storing the result of conversion in a seperate function called oct_num</span>
   oct_num <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">strconv<span class="token punctuation">.</span></span>FormatInt</span><span class="token punctuation">(</span>num<span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span>
   
   <span class="token comment">// printing the octal number</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Decimal number"</span><span class="token punctuation">,</span> num<span class="token punctuation">,</span> <span class="token string">"is converted to octal as"</span><span class="token punctuation">,</span> oct_num<span class="token punctuation">)</span>
   
   <span class="token comment">// taking another decimal number</span>
   num <span class="token operator">=</span> <span class="token number">34</span>
   
   <span class="token comment">// storing the result of conversion in a seperate function called oct_num</span>
   oct_num <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">strconv<span class="token punctuation">.</span></span>FormatInt</span><span class="token punctuation">(</span>num<span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span>
   
   <span class="token comment">// printing the octal number</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Decimal number"</span><span class="token punctuation">,</span> num<span class="token punctuation">,</span> <span class="token string">"is converted to octal as"</span><span class="token punctuation">,</span> oct_num<span class="token punctuation">)</span>
   
   <span class="token comment">//initialize the decimal value</span>
   num <span class="token operator">=</span> <span class="token number">26</span>
   
   <span class="token comment">// storing the result of conversion in a seperate function called oct_num</span>
   oct_num <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">strconv<span class="token punctuation">.</span></span>FormatInt</span><span class="token punctuation">(</span>num<span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span>
   
   <span class="token comment">// printing the octal number</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Decimal number"</span><span class="token punctuation">,</span> num<span class="token punctuation">,</span> <span class="token string">"is converted to octal as"</span><span class="token punctuation">,</span> oct_num<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GO LANGUAGE PROGRAM TO CONVERT DECIMAL NUMBER TO OCTAL USING FUNCTION</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
<span class="token punctuation">)</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token comment">//strconv package provides the required function to convert from decimal to octal</span>
<span class="token comment">// initializing and defining a function to convert decimal to octal</span>
func <span class="token function">toOctal</span><span class="token punctuation">(</span>number <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token keyword">int</span> <span class="token punctuation">{</span>

   <span class="token comment">// defining variables and assigning them values</span>
   octal <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">0</span>
   counter <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">1</span>
   remainder <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">0</span>
   <span class="token keyword">for</span> number <span class="token operator">!=</span> <span class="token number">0</span> <span class="token punctuation">{</span>
      remainder <span class="token operator">=</span> number <span class="token operator">%</span> <span class="token number">8</span>
      number <span class="token operator">=</span> number <span class="token operator">/</span> <span class="token number">8</span>
      octal <span class="token operator">+=</span> remainder <span class="token operator">*</span> counter
      counter <span class="token operator">*=</span> <span class="token number">10</span>
   <span class="token punctuation">}</span>
   <span class="token keyword">return</span> octal
<span class="token punctuation">}</span>
<span class="token comment">// this is the main function</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">var</span> num <span class="token keyword">int</span>
   
   <span class="token comment">//initialize the decimal value</span>
   num <span class="token operator">=</span> <span class="token number">154</span>
   
   <span class="token comment">// calling the toOctal() function and storing its result in oct_num() function</span>
   oct_num <span class="token operator">:</span><span class="token operator">=</span> <span class="token function">toOctal</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>
   
   <span class="token comment">// printing the octal number</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Decimal number"</span><span class="token punctuation">,</span> num<span class="token punctuation">,</span> <span class="token string">"is converted to octal as"</span><span class="token punctuation">,</span> oct_num<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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: 2022-11-14T11:53:21+05:30

908 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements