Python program for octal to decimal conversion

Octal To Decimal Conversion

Here, in this page we will discuss the program for octal to decimal conversion in python . Octal numbers are also called numbers with base 8 and hold values from 0 – 7. Whereas in Decimal numbers are with base 10 is Standard number system for denoting integers and non-integers. 

Algorithm for converting octal to decimal nmber

Methods discussed:

  1. Algorithmic way (Octal to Decimal)
  2. Inbuilt Method (Octal to Decimal)

Method 1 : Algorithmic Way

Algorithm :

  • Initialize two variable decimal value and base 0, 1 respectively
  • Iterate using while loop until num is greater then 0
  • For each iteration het the unit place digit and multiply it with base and add it to decimal value
  • divide num by 10 and multiply base with 8
Python ptogram for converting octal to decimal number

Python Code :

Run
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
    return decimal_value


octal = 512
print("The decimal value of",octal, " is",OctalToDecimal(octal))

Output :

The decimal value of 512 is 330

Method 2 : Inbuilt Method

By using inbuilt function int in Python we can convert any number with any base to decimal.

Python Code

Run
octal_num = 512

decimal_value = int(str(octal_num), 8)

print("The decimal value of {} is {}".format(octal_num, decimal_value))

Output

The decimal value of 512 is 330

3 comments on “Python program for octal to decimal conversion”


  • cherith

    n=int(input())
    l=list(str(n))
    l.reverse()
    decimal=0
    for i in range(0,len(l)):
    decimal+=int(l[i])*pow(8,i)
    print(decimal)


  • Sonal

    num=int(input(“Enter the octal number “))
    temp=num
    deci_val=0
    base=1
    while(temp):
    digit=temp%10
    deci_val+=digit*base
    base*=8
    temp//=10
    print(“The decimal value of {} is {}”.format(num,deci_val))


    • Love

      num_string=input()
      nums=[]
      for i in num_string:
      if i.isdigit():
      i=int(i)
      nums.append(i)
      length=len(nums)
      nums=nums[::-1]
      sums=0
      for i in range(0,length):
      nu=nums[i]

      nu=int(nu)
      result=nu*(8**(i))
      sums=sums+result
      print(sums)