An octal number is a kind of computer number system which is represented by 0 to 7 numbers whose base is 8. This number system allows only 0 to 7 numbers in its representation due to base 8. It means it does not allow a greater value of 7 in its number representations. Octal number systems use 3 bits of binary data to represent their numbers.
Otherside, A decimal number is a human friendly number which is represented by 0 to 9 numbers whose base is 10. The number of digits required to express a numeric value depends on the base value of the underlying number system.
For example, the decimal number system employs 10 digits 0 to 9 to represent any numeric value, while the octal number system requires 8 digits from 0 to 7.
Example: Following numbers are represented in the octal numbers whose base is 8.
(32)8, (125)8 , (150)8, (345)8, (246)8,
Example:Following numbers are represented in the decimal numbers whose base is 10.
(78)10, (65)10 , (831)10, (845)10, (144)10
Following table represents the octal number and its equivalent decimal number.
Octal Number | Equivalent Decimal Number |
(24)8 | (20)10 |
(55)8 | (45)10 |
(70)8 | (56)10 |
Why Is There A Need To Convert Octal to Decimal In Python
Decimal number is a general number system which is easily understood and represented by the computer users or programmers. Many times computer instructions are represented in octal numbers that require understanding for the programmers via converting octal to decimal numbers. There are many reasons where we require to convert octal number to a decimal number:
- Interpretation of Unix File Permissions: In the Unix operating system, file permissions are written in octal number. Though to understand these permission codes need to convert these into decimal form.
- Programmers Usage: In every programming language, programmers use decimal numbers to define different data and operations. Though, it also requires conversation of octal to decimal.
- System Code Interpretation: Computer processors execute instructions that may be presented by the octal number. But these numbers are not interpretable by the users. So, it is required to convert octal code into decimal numbers.
Approaches to Convert Octal to Decimal
To convert an octal number to decimal number, several approaches are available. There are some approaches listed below.
- Approach-1: Using Int() Function
- Approach-2: Using Recursion
- Approach-3: Using Iteration
- Approach-4: Using User Defined Function
Lets dive in details for each approach.
Approach-1: Using Int() Function
In Python language, we may convert octal numbers to decimal numbers using an in-built function named int(). Int() function takes a number as an argument with base 8 and returns an equivalent decimal number. Following algorithm describes the steps involved for the conversion of octal into its equivalent decimal number.
Algorithm:
Step-1: Take an octal number from the user and store it in the python variable.
Step-2: Then, int() function is called by passing an octal number as an argument.
Step-3: As a result, a decimal number is returned by the int() function.
To demonstrates this approach, a program is created which code is given below:
Code:
# Python program to demonstrate conversion of Octal to Decimal using int() function
octal=(input('Enter a Hexadecimal number: ')) # It ask octal code as an input from the users
decimal=int(octal,8) # Int() function convert given octal to decimal number
print('Original octal number is: ', octal) # It display original octal number
print('Decimal number equivalent to given Octal is: ', decimal) # It display resulted decimal number
Output:
Enter a octal number: 70
Decimal number equivalent to given octal is: 56
Code Explanation:
- The octal number is asked from the user and stores its value in the variable named octal.
- Int() function converts a given octal number to decimal by taking a number as an argument with base 8.
- The result of the function is stored in the variable named ‘Decimal’ that is the decimal number.
- The decimal number is displayed using the print function.
Approach-2: Using Recursion
An alternative approach of converting given octal numbers to decimal numbers is a recursive function. A recursive function calls itself until a condition is true. An algorithm for this approach is represented in the following steps.
Algorithm:
Step-1: User supply octal number as an input
Step-2: Octal_Decimal recursive function is created that calls recursively.
Step-3: Print decimal number as a result.
Code:
# Python program to demonstrate conversion of Octal to Decimal using recursive function
def Octal_Decimal(octal): # Recursive Function named Octal_Decimal() is defined
if octal==0: # Check whether octal is 0 or not
return 0
else:
return (octal % 10) + 8 * Octal_Decimal(octal // 10) #It recursively call Octal_Decimal() function
octal=int(input('Enter a octal number: ')) # It ask octal number as an input from the users
print('Original octal number is: ', octal) # It display original octal number
print('Decimal number equivalent to given octal number is :',Octal_Decimal(octal)) # Octal_Decimal() function is called
Output:
Enter a octal number: 456
Original octal number is: 456
Decimal number equivalent to given octal number is : 302
Code Explanation:
- An octal number asked from the user.
- Octal_Decimal() recursive function is created which takes octal number as an argument and calls itself until octal becomes 0.
- A decimal number equivalent to octal is printed.
Approach-3: Using Iteration
With the help of iterations, we can also convert octal numbers to decimal numbers. Here we use a while loop as iterations to do this conversion. This approach is defined in the following algorithm.
Algorithm:
Step-1: An octal number to be taken from the user.
Step-2: Divide octal number using modulus operator(%) to get the last digit.
Step-3: Divide octal number using division(/) operator to get the resulting octal number.
Step-4: Decimal number is calculated using addition of last digit multiplied by base.
Step-4 Base is calculated by multiplication current value of base with 8..
Step-5:Display the resulting decimal number.
Code:
# Python program to demonstrate conversion of Octal to Decimal using Iteration
decimal=0
base=1
octal=int(input('Enter an Octal number: ')) # It ask an octal number as an input from the users
oct=octal
while octal>0:
ld=octal%10
octal = int(octal / 10) # Divides octal by 10 using simple division operator
decimal += (ld * base)
base = base * 8
print('Original octal number is: ', oct) # It display original octal number
print('Decimal number equivalent to given octal number is :',decimal) # It display decimal number
Output:
Enter an Octal number: 77
Original octal number is: 77
Decimal number equivalent to given octal number is : 63
Code Explanation:
- It asks for an octal number from the user.
- Last digit of the octal number is computed using the modulus (%) operator and updated octal number by the division operator which divides it by 10.
- Decimal number is computed using addition of its current value with the last digit that multiplied by base.
- Base is computed using the current value of base multiplied by 8.
- Display decimal number equivalent to given octal number.
Approach-4: Using User Defined Function
In Python, with the help of user defined functions we can also convert an octal number to a decimal number. In this approach, an octal number is asked from the user and a decimal number is obtained. An algorithm for this approach is represented in the following steps.
Algorithm:
Step-1: Taking an octal number as an input from the user and storing the value of the given octal number into a variable.
Step-2: User defined function is created which takes an octal number as an argument and returns the resulting decimal number.
Step-4: Now, print the decimal number as the output equivalent to the given octal number.
Code:
# Python program to demonstrate conversion of Octal to Decimal using User Defined Function
def Oct_Dec(octal):
return(int(octal,8)) # It returns decimal number equivalent to given octal number
octal=(input('Enter an Octal number: ')) # It ask an octal number as an input from the users
decimal=Oct_Dec(octal) # A user defined function named Oct_Dec() is called here
print('Original octal number is: ', octal) # It display original octal number
print('Decimal number equivalent to given octal number is :',decimal) # It display decimal number
Output:
Enter an Octal number: 412
Original octal number is: 412
Decimal number equivalent to given octal number is : 266
Code Explanation:
- The octal number is asked from the user and stores its value in the octal variable.
- User defined function named Oct_Dec takes an octal number as an argument and returns a decimal number.
- The Resulting decimal number is displayed.
Best Approach- Int() Function
The best approach to convert an octal number to decimal number is an in-built function named int() . It is the best approach because:
- Multiple Number Conversion: It is used to convert any input number such as octal, binary, hexadecimal to decimal number.
- Reduce Code Length: It offers reducing length of code by replacing numbers of lines using format specifiers.
- Programmer Convenient: It is a short and very convenient way to convert hexadecimal to decimal numbers.
Sample problems related to convert octal to decimal
Sample Problem-1: Using Approach-1
Problem Definition: Implement a digital converter which converts an octal number to decimal representation. Then, create an algorithm and python code to demonstrate conversion of lists of octal numbers to decimal numbers.
Solution: An algorithm for this problem is represented in the following steps.
Step-1: User enters octal number that wants to convert into decimal.
Step-2: Next, the int() function is called which takes two arguments. First is an octal number and second one is base i.e. 8
Step-3: A decimal number is returned by the int() function.
Code:
# It demonstrate digital converter i.e. octal to decimal for list of octal number
octlallist=[234,675,310,120,255,655,712] # It create list of octal numbers
for i in octallist:
dec=int(i,8) # int() function convert given octal number to decimal
print('Decimal numbers equivalent to given octal numbers are: ', dec) # It display resulted decimal number
Output:
Enter a octal number: 234,675,310,120,255,655,712
Decimal numbers equivalent to given octals numbers are:156, 445, 200, 80, 173, 429, 458
Code Explanation:
- List of octal numbers has been created.
- Int() function converts each octal number of a list into decimal by taking a number as an argument with base 8.
- The result of the function is stored in the deci variable named that is the decimal number.
- A print() function displays the resulting decimal number.
Sample Problem-2: Using Approach-2
Problem Definition: Consider a computer programmer who wants to process computer instructions into decimal representation. Therefore create an algorithm and python code to demonstrate the conversion.
Solution: An algorithm for this approach is represented in the following steps.
Algorithm:
Step-1: Initially, users enter a computer instruction in octal number as an input.
Step-2: A recursive function is created that calls recursively to compute a decimal number.
Step-3: Equivalent decimal number is displayed.
Code:
# It process computer instructions through converting octal form to decimal representation
def Instruct_Dec(Instruct): # Recursive Function named Instruct_Dec() is defined
if Instruct==0: # Check whether octal is 0 or not
return 0
else:
return (Instruct % 10) + 8 * Instruct_Dec(INstruct // 10) #It recursively call Instruct_Dec() function
Instruct=int(input('Enter a computer instruction in octal form: ')) # It ask computer instruction in octal number as an input from the users
print('Computer instruction is: ', Instruct) # It print actual computer instruction in octal representation
print('Converted decimal number equivalent to given instruction is :',Instruct_Dec(Instruct) # Instruct_Dec() function is called
Output:
Enter a computer instruction in octal form: 56
Computer instruction is: 56
Converted decimal number equivalent to given instruction is: 70
Code Explanation:
- The user enters a computer instruction as an input.
- A recursive function named Instruct_Dec()is created which takes computer instruction in octal form as an argument and calls itself until its value becomes 0.
- A decimal form equivalent to computer instruction is returned and displayed.
Sample Problem-3:Using Approach-3
Problem Definition: Consider a company that wants to develop a digital calculator whose motive is to implement digital number system conversion. Digital calculator is able to convert one number format to another. For example to convert a decimal to binary, octal and hexadecimal as well as vice versa. Therefore create an algorithm and python code to demonstrate how to convert the following octal numbers to decimal numbers.
- (650)8
- (43)8
- (121)8
- (760)8
- (461)8
Solution: In this solution, we represent an algorithm, python code and explanation for this problem number 1.
Algorithm:
Step-1: An octal number is declared i.e. (650)8
Step-2: Division on octal number is performed using modulus operator(%) to get the last digit. Also divide the octal number using division(/) operator to update the value of octal number.
Step-3: A decimal number is computed using addition of the last digit multiplied by base.
Step-4 A base of octal is determined by the multiplication of current value of base with 8..
Step-5: A decimal number is displayed.
Code:
# This program convert octal number given in problem 1 the approach-3
decnum=0
octnum=650
while octnum>0:
enddigit=octnum%10
octnum = int(octnum / 10) # Divides octal by 10 using simple division operator
decnum += (enddigit * 8)
print('Decimal number equivalent to given octal number is :',decnum) # It display decimal number
Output:
Decimal number equivalent to given octal number is : 86
Code Explanation:
- Predefined octal number is initialized as an input using input() function.
- Further, the end digit of the octal number is determined using the modulus (%) operator and updated octal number by the division operator which divides it by 10.
- A decimal number is obtained using addition of its current value with the end digit that is multiplied by base value i.e. 8.
- A resulting decimal number equivalent to a given octal number is displayed.
Sample Problem-4: Using Approach-4
Problem Definition: Consider a unix file permission is written in the octal representation. Now, to understand this permission, users are required to convert octal to decimal. Therefore, create an algorithm and python code to demonstrate these conversions.
Solution: An algorithm for this problem is represented in the following steps.
Algorithm:
Step-1: A unix file permission code to be taken as input from the user using input() function and store it in UFP variable
Step-2: A user defined function is implemented that takes unix file permission code as an argument and returns the resulting decimal number.
Step-4: At last, a decimal number is printed.
Code:
# This program convert unix file permission represent into octal to decimal
def UnixFilePerCoverter(UFP):
return(int(UFP,8)) # It returns decimal number equivalent to given code
UFP=(input('Enter unix file permission code: ')) # It ask unix file permission code from the user
ProcessCode=UnixFilePerCoverter(UFP) # A user defined function named UnixFilePerConverter() is called here
print('Processed Code in decimal form equivalent to given octal number is :',ProcessCode) # It display result
Output:
Enter unix file permission code: 41
Processed Code in decimal form equivalent to given octal number is : 51
Code Explanation:
- A user entered a unix file permission code in octal number as an input.
- An UnixFilePerConverter() is a user-defined function that takes an octal number as an input argument and returns a decimal number as output.
- Then, display a resulted code
Conclusion
Octal number system represented using numbers 0 to 7 whose base is 8. It may also be converted into other number systems such as binary, hexadecimal and decimal. Python supports many number systems representations and their conversions using inbuilt functions and other approaches. In this article, many approaches to convert an octal number to a decimal number were presented with an algorithm, python code, output and its explanation. A built-in function named int() approach is considered as the best approach because it offers a shortened and convenient way.