Problem: Write a C program to convert a decimal number into an Octal number.

Example:

Decimal to Octal using While Loop

The following picture depicts the standard way of converting a number from decimal number system to octal number system.

Decimal to Octal

To implement the same in C, we divide the decimal number by 8 until the number reduces to 0 using while loop and in the meantime, we also concatenate each remainder according to bottom-up approach (i.e to the left of the concatenated remainder).

Output

Decimal to octal c output

Decimal to Octal using Recursion

The concept used in this method is the same as the above. The only difference is that, it is implemented using recursion.

Since, recursion implements stack, it is the best way to print remainder from bottom to top.

Output

Decimal to octal C output 2

In this tutorial, we learned to convert the decimal number to its octal representation using c programming language.

Leave a Reply