forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpositive_decimal_to_binary.py
More file actions
39 lines (35 loc) · 876 Bytes
/
positive_decimal_to_binary.py
File metadata and controls
39 lines (35 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#Function to convert a positive decimal number into its binary equivalent
'''
By using the double dabble method, append the remainder
to the list and divide the number by 2 till it is not
equal to zero
'''
def DecimalToBinary(num):
#the binary equivalent of 0 is 0000
if num == 0:
print('0000')
return
else:
binary = []
while num != 0:
rem = num % 2
binary.append(rem)
num = num // 2
#reverse the list and print it
binary.reverse()
for bit in binary:
print(bit, end="")
#executable code
decimal = int(input("Enter a decimal number to be converted to binary : "))
print("Binary number : ")
DecimalToBinary(decimal)
'''
Sample I/O :
Input :
Enter a decimal number to be converted into binary: 8
Output:
Binary number:
1000
Time Complexity : O(n)
Space Complexity : O(1)
'''