Python Program to Convert Octal Number to Decimal and vice-versa

In this python tutorial, you will learn how to Convert Octal Number to Decimal and vice-versa with the built in functions of the python programming language.

How to Convert Octal Number to Decimal and vice-versa ?

Let’s take a look at the first source code,  here the values are given as input by the user in the code, the built in functions carries out the function.

OCTAL TO DECIMAL

RUN CODE SNIPPET
#Python program to convert Octal Number to a decimal

octal_num = input("Enter an octal number :")
decimal_value = int(octal_num,8)
print("The decimal value of {} is {}".format(octal_num,decimal_value))

INPUT:

34

OUTPUT:

Enter an octal number :
The decimal value of 34 is 28
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable octal_num with the statements/strings ("Enter an octal number :")
  2. In the STDIN section of the code editor the input values are entered.
  3. We declare the variable decimal_value with the  function int(octal_num,8) where the int function is used to get the decimal value of the entered integer.
  4. Now, we display the output value using the print and display the statement/string ("The decimal value of {} is {}".format(octal_num,decimal_value)) .
  5. In the above statement/string, the variables {0} and {1} will hold the values of octal_num and decimal_value where the format function helps in variable substitution and data formatting.

Let’s take a look at the second source code,  here the values are given as input by the user in the code, the if statement and built in functions carry out the function.

DECIMAL TO OCTAL

RUN CODE SNIPPET
#Python Program to Convert Decimal Number to Octal

decimal_number = int(input("Enter a decimal number :"))
octal_number = oct(decimal_number).replace("0o", "")
print("\nThe octal value for {0} is {1}".format(decimal_number,octal_number ))

INPUT:

100

OUTPUT:

Enter a decimal number :
The octal value for 100 is 144
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable decimal_number with the statements/strings ("Enter a decimal number :"), we use the int function and declare the  input value as an integer value.
  2. In the STDIN section of the code editor the input values are entered.
  3. We declare the variable octal_number with the  function oct(decimal_number) along with the replace function which is used to replace the values ("0b","") with another value.
  4. Now, we display the output value using the print and display the statement/string ("\nThe octal value for {0} is {1}".format(decimal_number,octal_number )) .
  5. In the above statement/string, the variables {0} and {1} will hold the values of decimal_number and octal_number where the format function helps in variable substitution and data formatting.

NOTE:

  • The replace() function is an inbuilt function in the Python programming language that returns a copy of the string where all occurrences of a substring are replaced with another substring.
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  •  The oct() function is used to get an octal value of an integer number.
  •  The int() function to get the decimal value of the given integer.
  • The print() function prints the specified message to the screen, or other standard output device.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...