Summary: In this programming example, we will learn to convert a decimal number into an Octal number using loop and recursion in C.


Method 1: Using While Loop

Decimal to Octal

We can convert a decimal number to its octal representation, by dividing the decimal number by 8 and stacking the sequence of remainders in the bottom-up manner as shown in the above diagram.


Enter a Decimal number:
Octal: 13

In our program, we concatenate the remainders to the left of the octal to stack remainders in a bottom-up manner.

We are multiplying the remainder by 10s so then when it is added to the previous remainders (i.e. octal), it comes to its left.

Method 2: Using Recursion

We can also convert a decimal number into an octal representation by using recursion.


Enter a Decimal number:
Octal: 11

The approach used here is similar to the previous method, but instead of stacking the remainders, we’re directly outputting it in a bottom-up manner.

In this program, we have used head recursion. We are recursively calling the function before printing the remainder, so the remainder begins to print from the last recursive call, therefore getting output in bottom up manner.

These are the two ways using which we can convert any decimal number to its octal correspondent in the C programming language.

Leave a Reply

5 × three =