Skip to content

Commit 7b9f683

Browse files
authored
Create 2to00000010.py
1 parent 4433888 commit 7b9f683

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Decimal to binary/2to00000010.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# this uses GPL V3 LICENSE
2+
# code by @JymPatel
3+
4+
5+
binary = '$' # just starting var
6+
n = 15 # can get 2**16 numbers
7+
8+
# get integer as output which is less than limit 2**16
9+
try:
10+
input = int(input("What is your Decimal Number?"))
11+
limit = 2**(n + 1)
12+
input <= limit
13+
except ValueError:
14+
print("Please put integer in input! & less than", limit)
15+
sys.exit()
16+
17+
# main algorithm
18+
while n >= 0:
19+
if input < 2**n:
20+
binary = binary + '0'
21+
else:
22+
binary = binary + '1'
23+
input = input - 2**n
24+
n = n - 1
25+
26+
print(binary)
27+
28+
# get it at https://github.com/JymPatel/Python3-FirstEdition
29+
print("get it at https://github.com/JymPatel/Python3-FirstEdition")

0 commit comments

Comments
 (0)