-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinToDecConverter.py
More file actions
executable file
·43 lines (33 loc) · 1.05 KB
/
binToDecConverter.py
File metadata and controls
executable file
·43 lines (33 loc) · 1.05 KB
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
43
"""
Program to convert binary number into decimal and vce versa
"""
def binaryToDecimal(binaryNum):
decNum = 0
j=1
while(binaryNum !=0):
remainder = binaryNum%10
decNum=decNum+remainder*j
j=j*2
binaryNum = binaryNum/10
print "Equivalent decimal number : %ld" %decNum
def decimalToBinary(decimalNum):
binaryNum = ""
while(decimalNum>0):
binaryNum = str(decimalNum%2)+binaryNum
decimalNum>>=1
print "Equivalent Binary number: %s" %binaryNum
print "Usage:"
print "B - Convert Binary number to Decimal"
print "D - Convert Decimal number to Binary"
print "\nEnter your choice:"
choice = raw_input()
if (choice == 'B') | (choice =='b'):
print "Enter any Binary number:"
binaryNum = long (input())
binaryToDecimal(binaryNum)
elif (choice == 'D')|(choice == 'd'):
print "Enter any Decimal number:"
decimalNum = long (input())
decimalToBinary(decimalNum)
else:
print "You have entered wrong choice"