Home » Convert Octal to Decimal using Recursion in Python

Convert Octal to Decimal using Recursion in Python

Learn about Convert Octal to Decimal using Recursion in Python in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

Program to Convert Octal to Decimal using Recursion in Python

The below program coverts octal value to a decimal value using recursion. The function OctalDecimal() in the below code is a recursive function which returns the decimal value given a octal value.

Source code:

# Python program to convert octal to decimal using recursion

def OctalDecimal(num, i):
    if num >=0 and num <= 7:
        return num*pow(8, i)
    
    last_digit = num % 10
    return (pow(8, i) * last_digit) + OctalDecimal(num // 10, i+1)

# input from user
num = int(input('Enter an octal number: '))

# function call
print('The decimal value is =',OctalDecimal(num, 0))

Output:

Enter an octal number: 76
The decimal value is = 62

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Octal to Decimal in Python