mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Create octal_to_binary.py
Added ocatl_to_binary.py
This commit is contained in:
parent
ae0fc85401
commit
9da661b098
22
conversions/octal_to_binary.py
Normal file
22
conversions/octal_to_binary.py
Normal file
@ -0,0 +1,22 @@
|
||||
def octal_to_binary(octal):
|
||||
# Converting Octal to Decimal
|
||||
decimal = 0
|
||||
power = 0
|
||||
while octal != 0:
|
||||
decimal += (octal % 10) * pow(8, power)
|
||||
octal //= 10
|
||||
power += 1
|
||||
|
||||
# Converting Decimal to Binary
|
||||
binary = 0
|
||||
digit_place = 1
|
||||
while decimal != 0:
|
||||
binary += (decimal % 2) * digit_place
|
||||
decimal //= 2
|
||||
digit_place *= 10
|
||||
|
||||
return binary
|
||||
|
||||
octal_number = int(input("Enter octal number: "))
|
||||
binary_number = octal_to_binary(octal_number)
|
||||
print(f"The binary equivalent of {octal_number} is {binary_number}")
|
Loading…
x
Reference in New Issue
Block a user