Write a program in Python to convert an octal number to its corresponding decimal form.

Example:

Following is the standard mathematical way to convert an octal number into decimal.

Octal to Decimal

Let’s implement the steps into the python program using different methods.

Convert Octal to Decimal in Python using Loop

In this method, we will multiply each digit of the octal number to its corresponding value 8 raised to the power to its index (position from right – 1) and sum them up.

Output

Convert Octal to Decimal in Python using Recursion

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

On each recursive call, we need to pass the octal number by eliminating its last digit. The recursive call will stop when there is only a single left in the octal (i.e base condition).

Output

Do you like the methods?

Comment below your suggestions or doubts if you have any.

Leave a Reply

Close Menu