Professional Documents
Culture Documents
Prashant (20mts5742)
Prashant (20mts5742)
# Program to convert a decimal number in Radix 'R' and given radix to decimal
def val(c):
if c>='0' and c<='9':
return ord(c)-ord('0')
else:
return ord(c)-ord('A')+10;
def toDeci(str,base):
llen=len(str)
power=1
num=0
for i in range(llen-1,-1,-1):
if val(str[i])>=base:
print('Invalid Number')
return -1
num += val(str[i])*power
power=power*base
return num
def reVal(num):
if(num>=0 and num<=9):
return chr(num+ord('0'))
else:
return chr(num-10+ord('A'))
def fromDeci(base,inputNum):
res=''
while(inputNum>0):
res+=reVal(inputNum%base)
inputNum=int(inputNum/base)
res=res[::-1]
return res
def main():
print("\n--------------------MENU------------------\n"
"1.Binary_to_Decimal [OR] Decimal_to_binary\n"
"2.Binary_to_Octal [OR] Octal_to_Binary\n"
"3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary\n"
"4.Decimal_to_Octal [OR] Octal_to_Decimal\n"
"5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal\n"
"6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal\n"
"7.Exit")
Choice=int(input("Enter your choice: "))
if Choice==1:
print("--------Binary Decimal Conversion--------")
A1=int(input("Enter value of Radix'A': "))
B1=int(input("Enter value of Radix'B': "))
if (A1==2 and B1==10):
strr=input("Enter any binary number: ")
print(strr,'in decimal is',toDeci(strr,2))
main()
else:
strr=int(input("Enter any decimal number: "))
print(strr,"in binary is",fromDeci(2,strr))
main()
elif Choice==2:
print("--------Binary Octal Conversion---------")
A2=int(input("Enter value of Radix'A': "))
B2=int(input("Enter value of Radix'B': "))
if (A2==2 and B2==8):
strr=input("Enter any binary number: ")
newstrr=toDeci(strr,2)
print(strr,"in octal is",fromDeci(8,newstrr))
main()
else:
strr=input("Enter any octal number: ")
newstrr=toDeci(strr,8)
print(strr,"in binary is",fromDeci(2,newstrr))
main()
elif Choice==3:
print("---------Binary Hexadecimal Conversion--------")
A3=int(input("Enter value of Radix'A': "))
B3=int(input("Enter value of Radix'B': "))
if (A3==2 and B3==16):
strr=input("Enter any binary number: ")
newstrr=toDeci(strr,2)
print(strr,"in hexadecimal is",fromDeci(16,newstrr))
main()
else:
strr=input("Enter any hexadecimal number: ")
newstrr=toDeci(strr,16)
print(strr,"in binary is",fromDeci(2,newstrr))
main()
elif Choice==4:
print("---------Decimal Octal Conversion--------")
A4=int(input("Enter value of Radix'A': "))
B4=int(input("Enter value of Radix'B': "))
if (A4==10 and B4==8):
strr=input("Enter any decimal number: ")
newstrr=toDeci(strr,10)
print(strr,"in octal is",fromDeci(8,newstrr))
main()
else:
strr=input("Enter any octal number: ")
newstrr=toDeci(strr,8)
print(strr,"in decimal is",fromDeci(10,newstrr))
main()
elif Choice==5:
print("---------Decimal Hexadecimal Conversion--------")
A5=int(input("Enter value of Radix'A': "))
B5=int(input("Enter value of Radix'B': "))
if (A5==10 and B5==16):
strr=input("Enter any decimal number: ")
newstrr=toDeci(strr,10)
print(strr,"in hexadecimal is",fromDeci(16,newstrr))
main()
else:
strr=input("Enter any hexadecimal number: ")
newstrr=toDeci(strr,16)
print(strr,"in decimal is",fromDeci(10,newstrr))
main()
elif Choice==6:
print("---------Octal Hexadecimal Conversion--------")
A6=int(input("Enter value of Radix'A': "))
B6=int(input("Enter value of Radix'B': "))
if (A6==8 and B6==16):
strr=input("Enter any octal number: ")
newstrr=toDeci(strr,8)
print(strr,"in hexadecimal is",fromDeci(16,newstrr))
main()
else:
strr=input("Enter any hexadecimal number: ")
newstrr=toDeci(strr,16)
print(strr,"in octal is",fromDeci(8,newstrr))
main()
elif Choice==7:
print("Ending the program")
exit()
else:
print("\n Invalid Input.")
main()
main()
OUTPUT –
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 1
--------Binary Decimal Conversion--------
Enter value of Radix'A': 2
Enter value of Radix'B': 10
Enter any binary number: 11011011
11011011 in decimal is 219
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 1
--------Binary Decimal Conversion--------
Enter value of Radix'A': 10
Enter value of Radix'B': 2
Enter any decimal number: 100
100 in binary is 1100100
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 2
--------Binary Octal Conversion---------
Enter value of Radix'A': 2
Enter value of Radix'B': 8
Enter any binary number: 110
110 in octal is 6
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 2
--------Binary Octal Conversion---------
Enter value of Radix'A': 8
Enter value of Radix'B': 2
Enter any octal number: 233
233 in binary is 10011011
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 3
---------Binary Hexadecimal Conversion--------
Enter value of Radix'A': 2
Enter value of Radix'B': 16
Enter any binary number: 11111011101110010
11111011101110010 in hexadecimal is 1F772
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 3
---------Binary Hexadecimal Conversion--------
Enter value of Radix'A': 16
Enter value of Radix'B': 2
Enter any hexadecimal number: 3AB2
3AB2 in binary is 11101010110010
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 4
---------Decimal Octal Conversion--------
Enter value of Radix'A': 10
Enter value of Radix'B': 8
Enter any decimal number: 33
33 in octal is 41
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 4
---------Decimal Octal Conversion--------
Enter value of Radix'A': 8
Enter value of Radix'B': 10
Enter any octal number: 173
173 in decimal is 123
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 5
---------Decimal Hexadecimal Conversion--------
Enter value of Radix'A': 10
Enter value of Radix'B': 16
Enter any decimal number: 317547
317547 in hexadecimal is 4D86B
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 5
---------Decimal Hexadecimal Conversion--------
Enter value of Radix'A': 16
Enter value of Radix'B': 10
Enter any hexadecimal number: 1C2
1C2 in decimal is 450
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 6
---------Octal Hexadecimal Conversion--------
Enter value of Radix'A': 8
Enter value of Radix'B': 16
Enter any octal number: 123
123 in hexadecimal is 53
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 6
---------Octal Hexadecimal Conversion--------
Enter value of Radix'A': 16
Enter value of Radix'B': 8
Enter any hexadecimal number: AC3
AC3 in octal is 5303
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 7
# Q2Write a program that will prompt for the input of two integer values. Then using the Bitwise shift operators
show the result of
def main():
print("\n--------OPERATOR MENU---------\n"
"3. Exit")
if (ch==1):
x=input("Value 1: ")
y=input("Value 2: ")
main()
elif(ch==2):
x=input("Value 1: ")
y=input("Value 2: ")
main()
elif (ch==3):
print("Bye")
exit()
else:
main()
if __name__=="__main__":
main()
OUTPUT –
--------OPERATOR MENU---------
1. Left Shift.
2. Right Shift.
3. Exit
Enter your Choice: 1
Value 1: 2
Value 2: 1
Result: 4
--------OPERATOR MENU---------
1. Left Shift.
2. Right Shift.
3. Exit
Enter your Choice: 2
Value 1: 4
Value 2: 1
Result: 2
--------OPERATOR MENU---------
1. Left Shift.
2. Right Shift.
3. Exit
Enter your Choice: 3
Bye
# Q3 Write a program that will prompt for the input of two integer values Then using the logical operators show the
result of
print("\n-----OPERATOR MENU------\n"
if(ch==1):
x=input("value 1: ")
y=input("value 2: ")
main()
elif (ch==2):
x=input("value 1: ")
y=input("value 2: ")
elif(ch==3):
x=input("Value 1: ")
y=input("Value 2: ")
main()
elif (ch==4):
print("Bye")
exit()
else:
main()
if __name__== "__main__":
main()
OUTPUT –
-----OPERATOR MENU------
1. Bitwise XOR operator.
2. Bitwise AND operator.
3. Bitwise OR operator.
4. To Exit.
-----OPERATOR MENU------
1. Bitwise XOR operator.
2. Bitwise AND operator.
3. Bitwise OR operator.
4. To Exit.
-----OPERATOR MENU------
1. Bitwise XOR operator.
2. Bitwise AND operator.
3. Bitwise OR operator.
4. To Exit.
#1's compliment
def one_comp(l):
p=len(l)
r=""
for i in range(p):
if l[i]=='1':
r+='0'
else:
r+='1'
return r
def two_comp(A):
C=0
for i in range(len(A)):
if A[i]=="1":
C=i
else:
continue
D=A[:C]
E=A[C:]
ND=one_comp(D)
P=ND+E
print(P)
def menu():
if x==1:
print(one_comp(str(l)))
menu()
elif x==2:
two_comp(str(A))
menu()
elif x==3 :
exit()
else:
print("INVALID CHOICE")
menu()
menu()
OUTPUT –
def increment(counter):
for i, bit in enumerate(bits):# Iterate over bits and i is index start from 0
if bit == "0":
bits[i] = "1"
break
else:
bits[i] = "0"
def decrement(counter):
for i, bit in enumerate(bits):# Iterate over bits and i is index start from 0
if bit == "1":
bits[i] = "0"
break
else:
bits[i] = "1"
def main():
print("\n--------MENU----------\n"
"1.UP counter.\n"
"2.DOWN counter.\n"
"3.Exit.\n")
if choice==1:
counter=list(bits)
counter=increment(counter)
print(counter)
main()
elif choice==2:
counter=list(bits)
counter=decrement(counter)
print(counter)
main()
elif choice==3:
exit()
else:
main()
main()
OUTPUT –
--------MENU----------
1.UP counter.
2.DOWN counter.
3.Exit.
--------MENU----------
1.UP counter.
2.DOWN counter.
3.Exit.
def validatebin(m):
for i in m:
i=int(i)
if(i==0 or i==1):
continue
else:
print("\nError!! Please Enter a valid Binary base number.\n"
"\n--------Restarting the Program--------")
main()
def main():
print("\n---------------OPERATOR MENU---------------\n"
"1 Addition.\n"
"2. Exit.\n")
ch=int(input("Enter your Choice: "))
if(ch==1):
print()
print("Addition of 2 binary numbers")
x=input("1st Number : ")
validatebin(x)
y=input("2nd Number : ")
validatebin(y)
addbin(x,y)
main()
elif(ch==2):
exit()
else:
print("Wrong Input. Please try again.")
main()
main()
OUTPUT –
---------------OPERATOR MENU---------------
1 Addition.
2. Exit.
---------------OPERATOR MENU---------------
1 Addition.
2. Exit.
def one_comp(l):
p=len(l)
r=""
for i in range(p):
if l[i]=='1':
r+='0'
else:
r+='1'
return r
def two_comp(A):
C=0
for i in range(len(A)):
if A[i]=="1":
C=i
else:
continue
D=A[:C]
E=A[C:]
ND=one_comp(D)
P=ND+E
print(P)
return P
def addbin(a,b):
result=""
maxz=max(len(a),len(b))
a=a.zfill(maxz)
b=b.zfill(maxz)
carry=0
for i in range(maxz-1,-1,-1):
x=int(a[i])+int(b[i])+carry
if x==1 or x==0:
result+=str(x)
carry=0
elif x==2 :
result+="0"
carry=1
else :
result+="1"
carry=1
result=result[::-1]
if carry !=0 :
result=str(carry)+result
else:
def validatebin(m):
for i in m:
i=int(i)
if(i==0 or i==1):
continue
else:
main()
def main():
print("\n---------------OPERATOR MENU---------------\n"
"1 Subtraction.\n"
"2. Exit.\n")
if(ch==1):
print()
validatebin(x)
a=two_comp(y)
validatebin(y)
addbin(x,a)
main()
elif(ch==2):
exit()
else:
main()
main()
OUTPUT –
---------------OPERATOR MENU---------------
1 Subtraction.
2. Exit.
---------------OPERATOR MENU---------------
1 Subtraction.
2. Exit.
def conv(a,b):
temp=list(a)
for i in range(len(a)):
if b[i]=="1":
temp[i]="1"
else:
pass
a="".join(temp)
main()
def zfill(a,b):
maxz=max(len(a),len(b))
a=a.zfill(maxz)
b=b.zfill(maxz)
conv(a,b)
def validatebin(m):
for i in m:
if(i=='0' or i=='1'):
continue
else:
main()
def main():
print("\n------------------------------\n"
"2. To Exit.\n")
if(ch==1):
validatebin(a)
b=input("Enter your logic operand: ")
validatebin(b)
zfill(a,b)
elif(ch==2):
exit()
else:
main()
main()
OUTPUT –
------------------------------
1. Selective Set.
2. To Exit.
------------------------------
1. Selective Set.
2. To Exit.
def conv(a,b):
temp=list(a)
for i in range(len(a)):
if b[i]=="1":
temp[i]=flip(temp[i])
else:
pass
a="".join(temp)
main()
def flip(c):
if(c=='0'):
return '1'
else:
return '0'
def zfill(a,b):
maxz=max(len(a),len(b))
a=a.zfill(maxz)
b=b.zfill(maxz)
conv(a,b)
def validatebin(m):
for i in m:
if(i=='0' or i=='1'):
continue
else:
main()
def main():
print("\n------------------------------\n"
"1. Selective Complement.\n"
"2. To Exit.\n")
if(ch==1):
validatebin(a)
validatebin(b)
zfill(a,b)
elif(ch==2):
exit()
else:
main()
main()
OUTPUT –
------------------------------
1. Selective Complement.
2. To Exit.
------------------------------
1. Selective Complement.
2. To Exit.
def conv(a,b):
temp=list(a)
for i in range(len(a)):
if b[i]=="1":
temp[i]="0"
else:
pass
a="".join(temp)
print("New number is:",a)
main()
def zfill(a,b):
maxz=max(len(a),len(b))
a=a.zfill(maxz)
b=b.zfill(maxz)
conv(a,b)
def validatebin(m):
for i in m:
if(i=='0' or i=='1'):
continue
else:
print("\nPlease Enter a valid Binary base number.\n")
main()
def main():
print("\n------------------------------\n"
"1. Selective Clear.\n"
"2. To Exit.\n")
ch=int(input("Enter your Choice: "))
if(ch==1):
a=input("Enter your number : ")
validatebin(a)
b=input("Enter your logic operand: ")
validatebin(b)
zfill(a,b)
elif(ch==2):
print("\nEnding the Program. Have a Nice Day!!")
exit()
else:
print("\nWrong Input. Please try again.")
main()
main()
OUTPUT –
------------------------------
1. Selective Clear.
2. To Exit.
------------------------------
1. Selective Clear.
2. To Exit.