DEV Community

Saravanan Lakshmanan
Saravanan Lakshmanan

Posted on

Python Task

1. Find Binary to Decimal:

no = 11001
sum = 0
power = 0

while no>0:

    ld = no % 10
    no = no // 10
    decimal = ld * 2**power
    sum = sum + decimal
    power +=1
    # print(ld)

print(sum)
    # print(sum)
Enter fullscreen mode Exit fullscreen mode

Output:

25
Enter fullscreen mode Exit fullscreen mode

2. Find Decimal to Binary:

decimal = 5
binary = 0
place = 1

power = 0

while decimal > 0:
    rem = decimal % 2
    binary = binary + rem * 10**power
    decimal = decimal // 2
    power += 1

print('binary value:',binary)
Enter fullscreen mode Exit fullscreen mode

Output:

binary value: 101
Enter fullscreen mode Exit fullscreen mode

3. Find Binary to Octal:

no = 11001
binary = no
sum = 0
power = 0

while no>0:

    ld = no % 10
    no = no // 10
    decimal = ld * 2**power
    sum = sum + decimal
    power +=1

print(f'Decimal value of {binary} is {sum}')

decimal = sum
octal = 0
power = 0
while sum>0:
    rem = sum % 8 #1    
    sum = sum // 8   #3
    octal = octal + rem * 10**power
    power += 1
print(f'Octal value of {decimal} is {octal}')    
Enter fullscreen mode Exit fullscreen mode

Output:

Decimal value of 11001 is 25
Octal value of 25 is 31
Enter fullscreen mode Exit fullscreen mode

4. GCD/HCF: (TBD)

no1 = 75
no2 = 100

if no1 > no2:
    ln = no1
    sn = no2
else:
    ln = no2
    sn = no1

print('Largest number is',ln)        
print('Smallest number is',sn)     

while True:
    if ln % sn == 0:
        print('GCD is', sn)
        break
    else:
        rem = ln % sn
        ln = sn
        sn = rem
Enter fullscreen mode Exit fullscreen mode

Output:

Largest number is 100
Smallest number is 75
GCD is 25
Enter fullscreen mode Exit fullscreen mode

Top comments (0)