Skip to content

Commit 8608038

Browse files
committed
Decimal to Binary conversion using Stack
1 parent 791f6e7 commit 8608038

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Stack/P03_DecimalToBinary.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Author: OMKAR PATHAK
2+
3+
# Decimal to binary using Stack
4+
5+
import Stack
6+
7+
def dtob(decimal, base = 2):
8+
myStack = Stack.Stack()
9+
while decimal > 0:
10+
myStack.push(decimal % base)
11+
decimal //= base
12+
13+
result = ''
14+
while not myStack.isEmpty():
15+
result += str(myStack.pop())
16+
17+
return result
18+
19+
if __name__ == '__main__':
20+
print(dtob(15))

0 commit comments

Comments
 (0)