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

2 min read

The octal numeral system, or oct for short, is the base-8 number system and uses the digits 0 to 7. The main characteristic of an Octal Numbering System is that there are only 8 distinct counting digits from 0 to 7 with each digit having a weight or value of just 8 starting from the least significant bit (LSB).

In the decimal number system, each digit represents the different power of 10 making them base-10 number system.

Problem definition

Create a python program to convert an octal number into a decimal number.

Algorithm for octal to decimal conversion

  1. Take an octal number as input.
  2. Multiply each digit of the octal number starting from the last with the powers of 8 respectively.
  3. Add all the multiplied digits.
  4. 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

  1. Take a decimal number as input.
  2. Divide the input number by 8 and obtain its remainder and quotient.
  3. Repeat step 2 with the quotient obtained until the quotient becomes zero.
  4. 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

Recommended read:


PROGRAMS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read