Program to Convert BCD number into Decimal number | C ++ code to convert BCD to its decimal number ( base 10 ) . ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bcdToDecimal ( string s ) { int len = s . length ( ) , check = 0 , check0 = 0 ; int num = 0 , sum = 0 , mul = 1 , rev = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { sum += ( s [ i ] - '0' ) * 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 /= 10 ; } if ( check0 == 1 ) return rev - 1 ; return rev ; } int main ( ) { string s = "100000101000" ; cout << bcdToDecimal ( s ) ; return 0 ; }