- How to convert octal to decimal in python
- Support Our Site
- Python Program to Convert Octal Number to Decimal and vice-versa
- Problem definition
- Algorithm for octal to decimal conversion
- Program
- Output
- Program
- Output
- Problem definition
- Algorithm
- Program
- Output
- Program
- Output
- Python Octal to Decimal Conversion
- Octal and Decimal Numbers
- Octal Numbers
- Decimal Numbers
- Octal to Decimal Conversion
- Method 1: Using while loop
- Algorithm
- Method 2: Using for loop
- Algorithm
- Method 3: Recursive function
- Algorithm
- Octal to Decimal in Python
- Python Program to Convert Octal to Decimal
- Convert using for loop
- How to use Python to convert an octal to a decimal
- 4 Answers 4
- These first lines take any decimal number and convert it to any desired number base
- The second lines do the same but for a given range and a given decimal
- The third lines convert from any base back to decimal
How to convert octal to decimal in python
In this post, we will learn how to convert a octal value to decimal. This program will take the octal value as input from the user and convert it to decimal.
Octal number system is a base-8 number system. Each number is represented by the digits 0 to 7.
We can use our own algorithm or we can directly use int() to convert a octal value to decimal.
Algorithm to convert octal to decimal:
We need to follow the below steps to convert a octal value to decimal:
- Take the octal value as input from the user.
- Multiply each digits of the number with power of 8. The rightmost digit with 8^0, second right digit with 8^1 etc. Also, add all the values calculated.
- The total sum will result the required decimal value.
Below is the complete python program:
def octal_to_decimal(n): decimal = 0 multiplier = 1 while(n): digit = n % 10 n = int(n/10) decimal += digit * multiplier multiplier = multiplier * 8 return decimal no = int(input('Enter the octal value : ')) print('Decimal value is : <>'.format(octal_to_decimal(no)))
- octal_to_decimal method is used to convert a octal value to decimal. It takes one octal value and returns the decimal conversion.
- It uses a while loop that picks the rightmost digit and multiply with power of 8 and adds it to the decimal variable, which is the final decimal value.
- It asks the user to enter a number. It sores the number in the variable no and passes the value to octal_to_decimal to convert it to a decimal value.
If you run this program, it will print the below output:
We can also use int() with the octal number as the first argument and 8 as the second argument to convert the octal value to decimal.
= input('Enter the octal value : ') print('Decimal value is : <>'.format(int(no, 8)))
It will print similar output.
Support Our Site
To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site’s growth and development.
Python Program to Convert Octal Number to Decimal and vice-versa
In the decimal number system, each digit represents the different power of 10 making them base-10 number system.
Problem definition
Algorithm for octal to decimal conversion
- Take an octal number as input.
- Multiply each digit of the octal number starting from the last with the powers of 8 respectively.
- Add all the multiplied digits.
- The total sum gives the decimal number.
Program
num = input("Enter an octal number :") OctalToDecimal(int(num)) def OctalToDecimal(num): decimal_value = 0 base = 1 while (num): last_digit = num % 10 num = int(num / 10) decimal_value += last_digit * base base = base * 8 print("The decimal value is :",decimal_value)
Output
Enter an octal number :24 The decimal value is :20
In python, there is an alternate way to convert octal numbers into decimals using the int() method.
Program
octal_num = input("Enter an octal number :") decimal_value = int(octal_num,8) print("The decimal value of <> is <>".format(octal_num,decimal_value))
Output
Enter an octal number :24 The decimal value of 24 is 20
Problem definition
Create a python program to convert a decimal number into an octal number.
Algorithm
- Take a decimal number as input.
- Divide the input number by 8 and obtain its remainder and quotient.
- Repeat step 2 with the quotient obtained until the quotient becomes zero.
- The resultant is the octal value.
Program
decimal = int(input("Enter a decimal number :")) print("The octal equivalent is :",decimal_to_octal(decimal)) def decimal_to_octal(decimal): octal = 0 i = 1 while (decimal != 0): octal = octal + (decimal % 8) * i decimal = int(decimal / 8) i = i * 10 return octal
Output
Enter a decimal number :200 The octal equivalent is : 310
An alternate shorthand to convert a decimal number into octal is by using the oct() method.
Program
decimal_number = int(input("Enter a decimal number :")) octal_number = oct(decimal_number).replace("0o", "") print("The octal value for <> is <>".format(decimal_number,octal_number ))
Output
Enter a decimal number :200 The octal value for 200 is 310
Python Octal to Decimal Conversion
In this article, we will look at 4 different ways to convert octal to decimal in Python.
Octal and Decimal Numbers
Octal Numbers
Octal numbers are base 8 numbers. They are represented by the digits 0 to 7. The digits 8 and 9 are not used in octal numbers. Octal numbers are used in computer programming and computer hardware.
8 will be represented as 10 in octal, 9 will be represented as 11 in octal, and so on.
Decimal Numbers
Decimal numbers are base 10 numbers. They are represented by the digits 0 to 9. Decimal numbers are used in everyday life.
Octal to Decimal Conversion
Octal to decimal conversion is the process of converting octal numbers to decimal numbers. The process of octal to decimal conversion is similar to binary to decimal conversion. The only difference is that the base is 8 instead of 2.
Multiply each digit of the octal number with the corresponding power of 8. Then add all the products to get the decimal number.
Look at the image below to understand the process of octal to decimal conversion.
Method 1: Using while loop
To convert octal to decimal in Python, we can use a while loop and start from the rightmost digit. We can multiply each digit with the corresponding power of 8 and add all the products to get the decimal number.
Algorithm
- Initialize dec to 0 and i to 0. Here dec is the decimal number and i is the power of 8.
- Run a while loop until oct is greater than 0.
- Inside the while loop, add the product of oct%10 and 8**i to dec .
- Divide oct by 10 and increment i by 1.
- Return dec .
# function to convert octal to decimal def oct2dec(oct): dec = 0 i = 0 while oct > 0: dec += oct%10 * (8**i) oct //= 10 i += 1 return dec num = input("Enter a octal number: ") print(oct2dec(num))
Enter a octal number: 155 109
Method 2: Using for loop
We can also use for loop in a similar way.
To work with for loop we need to know the number of iterations in advance. Convert the octal number to a string using str() function. Then use len() function to get the number of digits in the octal number.
Algorithm
- Initialize dec to 0 and i to 0. Here dec is the decimal number and i is the power of 8.
- Convert oct to string using str() function.
- Get the number of digits in oct using len() function.
- Run a for loop from 0 to the number of digits in oct .
- Inside the for loop, add the product of oct[i] and 8**i to dec .
- Return dec .
# function to convert octal to decimal def oct2dec(oct): dec = 0 i = 0 oct = str(oct) for i in range(len(oct)): dec += int(oct[i]) * (8**i) return dec num = input("Enter a octal number: ") print(oct2dec(num))
Enter a octal number: 215 141
Method 3: Recursive function
Using recursion you can solve a problem that can be solved by solving a smaller version of the same problem.
For octal to decimal conversion, we can use recursion to solve the problem.
Algorithm
# recursive function to convert octal to decimal def oct2dec(oct): if oct == 0: return 0 else: return (oct%10) + 8 * oct2dec(oct // 10) num = input("Enter a octal number: ") print(oct2dec(num))
Enter a octal number: 65 53
Octal to Decimal in Python
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.
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))
How to use Python to convert an octal to a decimal
I had this little homework assignment and I needed to convert decimal to octal and then octal to decimal. I did the first part and could not figure out the second to save my life. The first part went like this:
decimal = int(input("Enter a decimal integer greater than 0: ")) print("Quotient Remainder Octal") bstring = " " while decimal > 0: remainder = decimal % 8 decimal = decimal // 8 bstring = str(remainder) + bstring print ("%5d%8d%12s" % (decimal, remainder, bstring)) print("The octal representation is", bstring)
4 Answers 4
If you want to return octal as a string then you might want to wrap it in str .
These first lines take any decimal number and convert it to any desired number base
def dec2base(): a = int(input('Enter decimal number: \t')) d = int(input('Enter expected base: \t')) b = "" while a != 0: x = '0123456789ABCDEF' c = a % d c1 = x[c] b = str(c1) + b a = int(a // d) return (b)
The second lines do the same but for a given range and a given decimal
def dec2base_R(): a = int(input('Enter start decimal number:\t')) e = int(input('Enter end decimal number:\t')) d = int(input('Enter expected base:\t')) for i in range (a, e): b = "" while i != 0: x = '0123456789ABCDEF' c = i % d c1 = x[c] b = str(c1) + b i = int(i // d) return (b)
The third lines convert from any base back to decimal
def todec(): c = int(input('Enter base of the number to convert to decimal:\t')) a = (input('Then enter the number:\t ')).upper() b = list(a) s = 0 x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'] for pos, digit in enumerate(b[-1::-1]): y = x.index(digit) if int(y)/c >= 1: print('Invalid input. ') break s = (int(y) * (c**pos)) + s return (s)
Note: I also have the GUI version if anyone needs them