Python Program to Converter a Decimal to Octal

Last Updated:

Converter a Number from Decimal to Octal

In the following example, we will convert a Decimal Number (100) to Octal Number (144).

Tips: It is recommended to use our online Decimal to Octal calculator for better understanding.

Example

Python Compiler
import math num = 23456 arr = [0]*50 answer = [0]*50 value = 1 count = -1 j = 0 while(num > value): count += 1 value = math.pow(8, count); arr[count] = value; count -= 1 print("Octal number of 23456 (decimal) is ", end="") for i in range(count, -1, -1): answer[j] = math.floor(num / arr[i]) j += 1 num = num % arr[i] for i in range(0, count+1): print(answer[i], end="")

Converter any Given Decimal Number to Octal Number

In the following example, we will convert any given decimal number to an octal number.

Example

Python Compiler
import math num = int(input("Enter a Decimal Number: ")); copyNum = num arr = [0]*50 answer = [0]*50 value = 1 count = -1 j = 0 while(copyNum > value): count += 1 value = math.pow(8, count); arr[count] = value; count -= 1 for i in range(count, -1, -1): answer[j] = math.floor(copyNum / arr[i]) j += 1 copyNum = copyNum % arr[i] print("Octal number of %d (decimal) is " % num, end="") for i in range(0, count+1): print(answer[i], end="")

Share this Page

Meet the Author