-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path26 binary, octal, hexa conversion.py
46 lines (36 loc) · 1.12 KB
/
26 binary, octal, hexa conversion.py
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
44
45
#---- 26 integer conversion to binary, octal and hexadecimal ----
def binary(num, rem, res):
if num > 1:
binary(num // 2, rem, res)
rem = num % 2
res = str(rem) + res
print(res , end = '' )
def octal(num, rem, res):
if num > 1:
octal(num // 8, rem, res)
rem = num % 8
res = str(rem) + res
print(res , end = '' )
def hexa(num, rem, res):
if num > 1:
hexa(num // 16, rem, res)
rem = num % 16
res = str(rem) + res
print(res , end = '' )
print('Binary, octal and hexadecimal conversion\n')
print('select an option from the menu: \n1. Binary conversion\n2. Octal conversion \n3. Hexadecimal conversion\n')
choice = int(input(': '))
rem = 0
res = ""
if choice == 1 :
num = int(input('enter a decimal number : '))
print('Binary : ')
binary(num, rem, res)
elif choice == 2 :
num = int(input('enter a decimal number : '))
print('Octal : ')
octal(num, rem, res)
elif choice == 3 :
num = int(input('enter a decimal number : '))
print('Hexa-decimal : ')
hexa(num, rem, res)