Learn about Octal to Decimal 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
Octal to Decimal in Python
The goal is to extract the digits of a given octal number beginning with the rightmost digit and store them in a variable called a decimal
variable. When extracting digits from an octal number, multiply the digit by the appropriate base (Power of 8) and add the result to the variable decimal
. Finally, the variable decimal
will be used to record the required decimal number.
Program:
# Python program to convert octal to decimal
def OctalDecimal(num):
decimal = 0
length = len(num)
for x in num:
length = length-1
decimal += pow(8,length) * int(x)
return decimal
# input number
num = input('Enter an octal number: ')
# function call
print('The decimal value is =',OctalDecimal(num))
Output:
Enter an octal number: 96
The decimal value is = 78
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Convert Decimal to Octal in Python