Write a python function to convert bcd to decimal


Topic: Write a python function to convert bcd to decimal

Solution

 
 def bcdToDecimal(s):  
    length = len(s); 
    check = 0; 
    check0 = 0; 
    num = 0; 
    sum = 0; 
    mul = 1; 
    rev = 0; 
    for i in range(length - 1, -1, -1): 
        sum += (ord(s[i]) - ord('0')) * mul; 
        mul *= 2; 
        check += 1; 
        if (check == 4 or i == 0): 
            if (sum == 0 and check0 == 0): 
                num = 1; 
                check0 = 1; 
            else: 
                num = num * 10 + sum;          
            check = 0; 
            sum = 0; 
            mul = 1; 
    while (num > 0): 
        rev = rev * 10 + (num % 10); 
        num //= 10;        
    if (check0 == 1): 
        return rev - 1;           
    return rev;  
 # write a python function to find all the sexy primes in a given range
def sexyprime(l, r) : 
    prime=[True] * (r + 1) 
    p = 2
    while(p * p <= r) : 
        if (prime[p] == True) :  
            for i in range( p * 2, r+1 ,p) : 
                   prime[i] = False
        p = p + 1
    for i in range( l,r - 6 + 1) : 
        if (prime[i] and prime[i + 6]) : 
            print("(", i , ",", i + 6,")", end="") 
            
 # write a python function to check whether the number is a duck number or not
 
def check_duck(num) : 
    n = len(num)   
    i = 0
    while (i < n and num[i] == '0') : 
        i = i + 1
    while (i < n) :  
        if (num[i] == "0") : 
            return True 
        i = i + 1
    return False
    
 # write a python function to check whether the given number is a Buzz number or not
 
 def isBuzz(num) : 
  
    return (num % 10 == 7 or num % 7 == 0)  
  
 # write a python function to check whether the number is a nude number or not
  
  def checkDivisbility(num): 
    digit = 0
    N = num 
    while (num != 0): 
        digit = num % 10
        num = num // 10
        if (digit == 0 or N % digit != 0): 
            return False
    return True
    
 # write a python function to check whether the number is a ugly number or not
 
 def isUgly(n): 
    if (n == 1): 
        return 1
    if (n <= 0): 
        return 0 
    if (n % 2 == 0): 
        return (isUgly(n // 2))       
    if (n % 3 == 0): 
        return (isUgly(n // 3))   
    if (n % 5 == 0): 
        return (isUgly(n // 5)) 
    return 0
    
 # write a python function to write a prime number as the sum of two composite numbers
 
 def findNums(n): 
    if (n <= 11): 
        if (n == 8): 
            print("4 4", end = " ") 
        if (n == 10): 
            print("4 6", end = " ") 
        else: 
            print("-1", end = " ") 
    if (n % 2 == 0): 
        print("4 ", (n - 4), end = " ") 
    else: 
        print("9 ", n - 9, end = " ") 
        
 # write a python function to print two composite numbers whose difference is N
 
 def find_composite_nos(n) :  
    print(9 * n, 8 * n);  
    
 # write a python function to print N-bonacci series
 
 def bonacciseries(n, m) :  
    a = [0] * m 
    a[n - 1] = 1 
    for i in range(n, m) : 
        for j in range(i - n, i) : 
            a[i] = a[i] + a[j]  
    for i in range(0, m) : 
        print (a[i], end = " ") 
     



List all Python Programs