Skip to content

Commit 35b92f8

Browse files
committed
Added program to convert any number of any base to the required base from base 2 to 32
1 parent fd3130b commit 35b92f8

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import datetime
2+
print("Today's Date and time is :", end=" ")
3+
print(str(datetime.datetime.now()))
4+
print("")
5+
print("------This program converts number with any base to the base entered by the User with guaranteed accuracy of minimum 3 decimal places.------")
6+
print(" Note: base should be less than or equal to 32 and greater than equal to 2 ")
7+
print("")
8+
print("**It rounds off the number upto 20th decimal places**")
9+
print("")
10+
tsssy=""
11+
while(tsssy==""):
12+
n=int(input("Enter base for the number"))
13+
print("")
14+
dic={'a':10,'b':11,'c':12,'d':13,'e':14,'f':15,'g':16,'h':17,'i':18,'j':19,'k':20,'l':21,'m':22,'n':23,'o':24,'p':25,'q':26,'r':27,'s':28,'t':29,'u':30,'v':31}
15+
rttu=""
16+
if n>10:
17+
xyzA=12
18+
while(xyzA>0):
19+
asd=[]
20+
akd=[]
21+
m=input("Enter Number with base "+str(n)).lower()
22+
print("")
23+
gg="".join(m)
24+
gg=gg.split(".")
25+
m=gg[0]
26+
xyz=0
27+
tty=0
28+
ks=0
29+
if len(gg)==2:
30+
for kjj in range(len(gg[1])):
31+
if 97<=ord(gg[1][kjj])<=122:
32+
u=gg[1][kjj]
33+
ks=dic[u]
34+
if int(ks)>=n:
35+
xyz=-1
36+
else:
37+
akd.append(str(ks))
38+
else:
39+
akd.append(gg[1][kjj])
40+
41+
42+
43+
for i in range(len(m)):
44+
if 97<=ord(m[i])<=122:
45+
u=m[i]
46+
ks=dic[u]
47+
if int(ks)>=n:
48+
xyz=-1
49+
else:
50+
asd.append(str(ks))
51+
else:
52+
asd.append(m[i])
53+
if xyz==-1:
54+
xyzA=12
55+
print("Enter number again correctly, Make sure digits for the number should be less than the base of the number")
56+
else:
57+
xyzA=-9
58+
59+
if n<=10:
60+
xyzA=12
61+
while(xyzA>0):
62+
asd=[]
63+
akd=[]
64+
m=input("Enter Number with base "+str(n)).lower()
65+
print("")
66+
gg="".join(m)
67+
gg=gg.split(".")
68+
m=gg[0]
69+
xyz=0
70+
ks=0
71+
if len(gg)==2:
72+
for kjj in range(len(gg[1])):
73+
ks=gg[1][kjj]
74+
if int(ks)>=n:
75+
xyz=-1
76+
77+
else:
78+
akd.append(gg[1][kjj])
79+
for i in range(len(m)):
80+
if int(m[i])>=n:
81+
xyz=-1
82+
else:
83+
asd.append(m[i])
84+
if xyz==-1:
85+
xyzA=12
86+
print("Enter number again correctly, Make sure digits for the number should be less than the base of the number")
87+
else:
88+
xyzA=-9
89+
lm=int(input("Enter base to which you want to convert the given number"))
90+
print("")
91+
c=0
92+
d=0
93+
e=-1
94+
f=0
95+
t=1
96+
y=""
97+
if len(gg)==2:
98+
hj=1
99+
uu=1
100+
101+
for ty in range(len(akd)):
102+
f+=int(akd[ty])*(n**e)
103+
e-=1
104+
105+
while(hj<=20):
106+
uu=f*lm
107+
108+
109+
if int(uu)>=10:
110+
for i in range(97,119):
111+
if dic[chr(i)]==int(uu):
112+
rttu+=chr(i)
113+
if int(uu)<10:
114+
rttu+=str(int(uu))
115+
f=uu-int(uu)
116+
hj+=1
117+
118+
119+
for i in range(len(asd)-1,-1,-1):
120+
c+=int(asd[i])*(n**d)
121+
d+=1
122+
123+
while(t>0):
124+
ggg=c%lm
125+
if int(ggg)>=10:
126+
for iu in range(97,119):
127+
if dic[chr(iu)]==int(ggg):
128+
y+=chr(iu)
129+
if int(ggg)<10:
130+
y+=str(c%lm)
131+
c=c//lm
132+
if c==0:
133+
t=-1
134+
print("Number with base "+str(n)+" was "+".".join(gg)+" .Now it is converted into base "+str(lm)+" the required number is:",end=" ")
135+
print(y[::-1],end=".")
136+
if len(rttu)!=0:
137+
138+
print(rttu)
139+
if len(rttu)==0:
140+
print(0)
141+
print("")
142+
uy=input("Press Enter to continue or hit any key to Exit !")
143+
144+
145+

0 commit comments

Comments
 (0)