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

Example:

Method 1: Using While Loop

Octal to Decimal

We can convert an octal number to its corresponding decimal form by multiplying each of its digits to its respective exponential value of 8 (i.e. 8position_from_the_right – 1) and adding the respective results.

In our program, we implement it using a while loop. Inside the loop, we extract the digits, multiply them with their respective value of 8x, and add them together to get the decimal representation of the input octal number.

Output:

Enter an Octal number:
14
Decimal: 12


Method 2: Using Recursion

We can also convert an octal number to its decimal correspondent using recursion.

Output:

Enter an Octal number:
56
Decimal: 46


The approach used in this method is the same as the above program but is implemented using recursion.

In the function, we extract the digit (last digit) using the modulus operator, multiply it with its respective value of 8i and add it to the next recursive result.

On every recursive call, we pass the octal number after eliminating the last digit.

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

Leave a Reply

fourteen + 3 =