Program to Convert BCD number into Decimal number | Function to convert BCD to Decimal ; Iterating through the bits backwards ; Forming the equivalent digit ( 0 to 9 ) from the group of 4. ; Reinitialize all variables and compute the number . ; Update the answer ; Reverse the number formed . ; Driver Code ; Function Call
function bcdToDecimal ( s ) { let len = s . length ; let check = 0 , check0 = 0 ; let num = 0 , sum = 0 ; let mul = 1 , rev = 0 ; for ( let i = len - 1 ; i >= 0 ; i -- ) { sum += ( s [ i ] - ' ' ) * mul ; mul *= 2 ; check ++ ; if ( check == 4 i == 0 ) { if ( sum == 0 && 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 = Math . floor ( num / 10 ) ; } if ( check0 == 1 ) return rev - 1 ; return rev ; } let s = " " ; document . write ( bcdToDecimal ( s . split ( ' ' ) ) ) ;