Octal to Decimal in Python | In Computer Science, generally Octal number system used to store big data values. The octal system is the base 8 number system. We can also convert from Binary to Decimal, Decimal to Binary and Decimal to Octal, Octal to Binary and Binary to Octal also can be done.

In this post, we will write the programs for octal to decimal conversion in Python language. We will be given a decimal number and the python program to convert the given octal number into an equivalent decimal number.

Octal | Decimal |

1 | 1 |

2 | 2 |

3 | 3 |

4 | 4 |

5 | 5 |

6 | 6 |

7 | 7 |

10 | 8 |

11 | 9 |

12 | 10 |

Note:- 8 & 9 are not present in the Octal number system.

## Python Program to Convert Octal to Decimal

This python program using a while loop to convert octal to decimal. We can also take the help of a user-defined function. A function is a block of code that performs a specific task. We will take an octal number while declaring the variables. Python program to convert octal to a decimal using while loop and finally, the result will be displayed on the screen.

```
# Python program to convert octal to decimal
def OctalDecimal(num): #user-defined function
decimal = 0
base = 1 #Initializing base value to 1, i.e 8^0
while (num):
# Extracting last digit
last_digit = num % 10
num = int(num / 10)
decimal += last_digit * base
base = base * 8
return decimal
# take inputs
num = int(input('Enter an octal number: '))
# calling function and display result
print('The decimal value is =',OctalDecimal(num))
```

Output for the different input values:-

Enter an octal number: 25

The decimal value is = 21

Enter an octal number: 10

The decimal value is = 8

Enter an octal number: 2544

The decimal value is = 1380

## Convert using for loop

In the previous program, convert octal to a decimal using a while loop but in this program, convert octal to a decimal using for loop. This program uses string variables instead of integers to store octal values.

```
# Python program to convert octal to decimal
def OctalDecimal(num): #user-defined function
decimal = 0
length = len(num)
for x in num:
length = length-1
decimal += pow(8,length) * int(x)
return decimal
# take inputs
num = input('Enter an octal number: ')
# calling function and display result
print('The decimal value is =',OctalDecimal(num))
```

Output:-

Enter an octal number: 56

The decimal value is = 46

## Convert Octal to Decimal using Recursion

A function/method that contains a call to itself is called the recursive function/method. A technique of defining the recursive function/method is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.

```
# Python program to convert octal to decimal using recursion
def OctalDecimal(num, i): #user-defined function
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)
# take inputs
num = int(input('Enter an octal number: '))
# calling function and display result
print('The decimal value is =',OctalDecimal(num, 0))
```

Output:-

Enter an octal number: 215

The decimal value is = 141

## Convert Octal to Decimal in Python

This is the simplest and easiest program in python because this program is using a pre-defined function. We will take the octal number when declaring the variable and print the decimal value of the number using the int() method.

```
# Python program to convert octal to decimal using int() methods
def OctalDecimal(n): #user-defined function
return int(n, 8)
# take inputs
num = input('Enter an octal number: ')
# calling function and display result
print('The decimal value is =',OctalDecimal(num))
```

Output:-

Enter an octal number: 100

The decimal value is = 64

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!