Octal To Decimal Conversion In Python
Published on August 17 2023
In this tutorial you will see how to convert octal numbers to decimal numbers using logical implementation with string. Also, we will use built in methods and understand each line in detail.
Source code
# Method - 1 - using custom logic
octal_value = int(input("Enter any octal value - only allowed is 0-7:"))
def octal_to_decimal(octalVal):
position = 0
decimal_number = 0
while octalVal != 0:
decimal = octalVal % 10
decimal_number = decimal_number + decimal * pow(8, position)
octalVal = octalVal // 10
position = position + 1
return decimal_number
output = octal_to_decimal(octal_value)
print('Decimal value:', output)
# Method - 2 - using built-in function - int()
octal_value = input("Enter any octal value - only allowed is 0-7:")
output = int(octal_value, 8)
print('Decimal value:', output)