MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / decimal_to_octal

Function decimal_to_octal

conversions/decimal_to_octal.py:9–24  ·  view source on GitHub ↗

Convert a Decimal Number to an Octal Number. >>> all(decimal_to_octal(i) == oct(i) for i ... in (0, 2, 8, 64, 65, 216, 255, 256, 512)) True

(num: int)

Source from the content-addressed store, hash-verified

7
8
9def decimal_to_octal(num: int) -> str:
10 """Convert a Decimal Number to an Octal Number.
11
12 >>> all(decimal_to_octal(i) == oct(i) for i
13 ... in (0, 2, 8, 64, 65, 216, 255, 256, 512))
14 True
15 """
16 octal = 0
17 counter = 0
18 while num > 0:
19 remainder = num % 8
20 octal = octal + (remainder * math.floor(math.pow(10, counter)))
21 counter += 1
22 num = math.floor(num / 8) # basically /= 8 without remainder if any
23 # This formatting removes trailing '.0' from `octal`.
24 return f"0o{int(octal)}"
25
26
27def main() -> None:

Callers 1

mainFunction · 0.85

Calls 1

floorMethod · 0.80

Tested by

no test coverage detected