**Jump To Right Section**Show

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

- 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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

1 2 3 4 | 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 alternate way

1 2 3 4 5 | 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

1 2 3 4 | Enter an octal number :24 The decimal value of 24 is 20 |

#### Problem definition: Convert Decimal to Octal

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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

1 2 3 4 5 | 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 alternate way

1 2 3 4 5 | 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

1 2 3 4 | Enter a decimal number :200 The octal value for 200 is 310 |

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

## Leave a Comment