Skip to content

Commit ad7f89f

Browse files
author
amir mohammad
committed
resolving issue 54
1 parent 85feeff commit ad7f89f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

convert_decimal_to_binary.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Convert a decimal number to a binary number
3+
"""
4+
5+
try:
6+
decimal_num = int(input("Enter a decimal number: "))
7+
bin = 0
8+
ctr = 0
9+
tmp = decimal_num
10+
11+
except Exception as error:
12+
print("An error has occurred! => ", str(error))
13+
exit()
14+
15+
else:
16+
while(tmp > 0):
17+
bin = ((tmp % 2) * (10 ** ctr)) + bin
18+
tmp = int(tmp / 2)
19+
ctr += 1
20+
21+
print("Binary of {x} is: {y}".format(x = decimal_num, y = bin))
22+
23+

0 commit comments

Comments
 (0)