Skip to content

Commit 1346afc

Browse files
authored
Create decimal_to_binary_converter.py
I came up with this algorithm to convert decimal(natural numbers) to binary completely from scratch and therefore it might not be the best most efficient implementation. Optimizations are welcome.
1 parent a2dc331 commit 1346afc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

math/decimal_to_binary_converter.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
def dec_to_bin(n):
3+
quo = n
4+
binary = 0
5+
while (quo>0):
6+
tmp = quo
7+
pows = 0
8+
while (quo > 1):
9+
quo = quo // 2
10+
pows = pows + 1
11+
quo = tmp-pow(2,pows)
12+
binary = binary+pow(10,pows)
13+
return binary
14+
15+
for i in range(0,101):
16+
print(dec_to_bin(i))

0 commit comments

Comments
 (0)