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

**Example:**

1 2 3 4 5 | Input: 14 Output: 12 Input: 5 Output: 5 |

Following is the standard mathematical way to convert an octal number into 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.

1 2 3 4 5 6 7 8 9 10 11 12 | octal = input('Enter a Octal number: ') decimal = 0 #length of octal input l = len(octal) #loop through each digit of binary for x in octal: l = l-1 #decrease l by 1 decimal += pow(8,l) * int(x) #multiply print("Decimal of {p} is {q} ".format(p=octal, q=decimal)) |

**Output**

1 2 | Enter a Octal number: 14 Decimal of 14 is 12 |

## 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).

1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #'i' represents the nth recursive call as well as #nth digit from left, so it can be used as an exponent def octToDec(oct, i): if oct >= 0 and oct <= 7: return oct*pow(8,i) lastDigit = oct%10; return (pow(8,i) * lastDigit) + octToDec(oct//10, i+1); octal = int(input('Enter a Octal number: ')) decimal = octToDec(octal,0) print("Decimal of {p} is {q} ".format(p=octal, q=decimal)) |

**Output**

1 2 | Enter a Octal number: 12 Decimal of 12 is 10 |

Do you like the methods?

Comment below your suggestions or doubts if you have any.