In this article, We make a python program to convert Octal to Decimal and vice-versa. Let’s start.

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: Convert Octal to Decimal

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

Algorithm
  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

Output

In python, there is an alternate way to convert octal numbers into decimals using the int() method.

Program alternate way

Output

 

Problem definition: Convert Decimal to Octal

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

Output

An alternate shorthand to convert a decimal number into octal is by using the oct() method.

Program alternate way

Output

Recommended read: Check If A Number Is A Perfect Square Or Not

Leave a Comment