-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecToBin.py
More file actions
47 lines (34 loc) · 889 Bytes
/
DecToBin.py
File metadata and controls
47 lines (34 loc) · 889 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
40
41
42
''' #[Working]
#Using built-in decimal to binary conversion function
def decimalToBinary(n):
return bin(n).replace("0b", "")
if __name__ == '__main__':
num = int(input("Enter a decimal number: "))
print( "Binary equivalent: ",decimalToBinary(num))
'''
'''
#Using Recursive Algorithm
def DecToBin(num):
if num > 1:
DecToBin(num // 2)
num = num % 2
print([num])
numVal = int(input("Enter a decimal number: "))
DecToBin(numVal)
'''
#Using Recursive Algorithm
listNum = []
class DerivedList(list):
def insertAtLastLocation(self,obj):
self.__iadd__([obj])
lst=DerivedList(listNum)
def DecToBin(num):
while num > 1:
DecToBin(num // 2)
num = num % 2
lst.insertAtLastLocation(num)
def numVal():
numVal = int(input("Enter a decimal number: "))
DecToBin(numVal)
numVal()
print(lst)