Program to Convert BCD number into Decimal number

14 May, 2021
Given a BCD (Binary Coded Decimal) number, the task is to convert the BCD number into its equivalent Decimal number. 
Examples:  
Input: BCD = 100000101000 Output: 828 Explanation: Dividing the number into chunks of 4, it becomes 1000 0010 1000. Here, 1000 is equivalent to 8 and 0010 is equivalent to 2. So, the number becomes 828.
Input: BCD = 1001000 Output: 48 Explanation: Dividing the number into chunks of 4, it becomes 0100 1000. Here, 0100 is equivalent to 4 and 1000 is equivalent to 8. So, the number becomes 48.  
Approach:  
Iterate over all bits in given BCD numbers.Divide the given BCD number into chunks of 4, and start computing its equivalent Decimal number. Store this number formed in a variable named sum. Start framing a number from the digits stored in the sum in a variable num. Reverse the number formed so far and return that number.  
Iterate over all bits in given BCD numbers.
Divide the given BCD number into chunks of 4, and start computing its equivalent Decimal number. 
Store this number formed in a variable named sum. 
Start framing a number from the digits stored in the sum in a variable num. 
Reverse the number formed so far and return that number.  
Below is the implementation of the above approach.  
C++
Java
Python3
C#
Javascript
// C++ code to convert BCD to its// decimal number(base 10). // Including Header Files#include <bits/stdc++.h>using namespace std; // Function to convert BCD to Decimalint bcdToDecimal(string s){    int len = s.length(),        check = 0, check0 = 0;    int num = 0, sum = 0,        mul = 1, rev = 0;     // Iterating through the bits backwards    for (int i = len - 1; i >= 0; i--) {         // Forming the equivalent        // digit(0 to 9)        // from the group of 4.        sum += (s[i] - '0') * mul;        mul *= 2;        check++;         // Reinitialize all variables        // and compute the number.        if (check == 4 || i == 0) {            if (sum == 0 && check0 == 0) {                num = 1;                check0 = 1;            }            else {                // update the answer                num = num * 10 + sum;            }             check = 0;            sum = 0;            mul = 1;        }    }     // Reverse the number formed.    while (num > 0) {        rev = rev * 10 + (num % 10);        num /= 10;    }     if (check0 == 1)        return rev - 1;     return rev;} // Driver Codeint main(){    string s = "100000101000";     // Function Call    cout << bcdToDecimal(s);     return 0;}
// Java code to convert BCD to its// decimal number(base 10).// Including Header Filesimport java.io.*;import java.util.*; class GFG {     // Function to convert BCD to Decimalpublic static int bcdToDecimal(String s){    int len = s.length();    int check = 0, check0 = 0;    int num = 0, sum = 0;    int mul = 1, rev = 0;     // Iterating through the bits backwards    for(int i = len - 1; i >= 0; i--)    {        // Forming the equivalent       // digit(0 to 9)       // from the group of 4.       sum += (s.charAt(i) - '0') * mul;       mul *= 2;       check++;               // Reinitialize all variables       // and compute the number.       if (check == 4 || i == 0)       {           if (sum == 0 && check0 == 0)           {               num = 1;               check0 = 1;           }           else           {                // Update the answer               num = num * 10 + sum;           }                       check = 0;           sum = 0;           mul = 1;       }    }         // Reverse the number formed.    while (num > 0)    {        rev = rev * 10 + (num % 10);        num /= 10;    }     if (check0 == 1)        return rev - 1;     return rev;} // Driver codepublic static void main(String[] args){    String s = "100000101000";     // Function Call    System.out.println(bcdToDecimal(s));}} // This code is contributed by coder001
# Python3 code to convert BCD to its# decimal number(base 10). # Function to convert BCD to Decimaldef bcdToDecimal(s):     length = len(s);    check = 0;    check0 = 0;    num = 0;    sum = 0;    mul = 1;    rev = 0;         # Iterating through the bits backwards    for i in range(length - 1, -1, -1):                 # Forming the equivalent        # digit(0 to 9)        # from the group of 4.        sum += (ord(s[i]) - ord('0')) * mul;        mul *= 2;        check += 1;                 # Reinitialize all variables        # and compute the number        if (check == 4 or i == 0):            if (sum == 0 and check0 == 0):                num = 1;                check0 = 1;                             else:                                 # Update the answer                num = num * 10 + sum;                             check = 0;            sum = 0;            mul = 1;                 # Reverse the number formed.    while (num > 0):        rev = rev * 10 + (num % 10);        num //= 10;             if (check0 == 1):        return rev - 1;             return rev; # Driver Codeif __name__ == "__main__":     s = "100000101000";         # Function Call    print(bcdToDecimal(s));     # This code is contributed by AnkitRai01
// C# code to convert BCD to its// decimal number(base 10).// Including Header Filesusing System; class GFG{     // Function to convert BCD to Decimalpublic static int bcdToDecimal(String s){    int len = s.Length;    int check = 0, check0 = 0;    int num = 0, sum = 0;    int mul = 1, rev = 0;     // Iterating through the bits backwards    for(int i = len - 1; i >= 0; i--)    {         // Forming the equivalent        // digit(0 to 9)        // from the group of 4.        sum += (s[i] - '0') * mul;        mul *= 2;        check++;                     // Reinitialize all variables        // and compute the number.        if (check == 4 || i == 0)        {            if (sum == 0 && check0 == 0)            {                num = 1;                check0 = 1;            }            else            {                     // Update the answer                num = num * 10 + sum;            }                         check = 0;            sum = 0;            mul = 1;        }    }         // Reverse the number formed.    while (num > 0)    {        rev = rev * 10 + (num % 10);        num /= 10;    }     if (check0 == 1)        return rev - 1;     return rev;} // Driver codepublic static void Main(String[] args){    String s = "100000101000";     // Function Call    Console.WriteLine(bcdToDecimal(s));}} // This code is contributed by 29AjayKumar
<script> // Javascript code to convert BCD to its// decimal number(base 10).// Including Header Files // Function to convert BCD to Decimalfunction bcdToDecimal(s){    let len = s.length;    let check = 0, check0 = 0;    let num = 0, sum = 0;    let mul = 1, rev = 0;      // Iterating through the bits backwards    for(let i = len - 1; i >= 0; i--)    {         // Forming the equivalent       // digit(0 to 9)       // from the group of 4.       sum += (s[i] - '0') * mul;       mul *= 2;       check++;                // Reinitialize all variables       // and compute the number.       if (check == 4 || i == 0)       {           if (sum == 0 && check0 == 0)           {               num = 1;               check0 = 1;           }           else           {                 // Update the answer               num = num * 10 + sum;           }                        check = 0;           sum = 0;           mul = 1;       }    }          // Reverse the number formed.    while (num > 0)    {        rev = rev * 10 + (num % 10);        num = Math.floor(num / 10);    }      if (check0 == 1)        return rev - 1;      return rev;} // Driver Code         let s = "100000101000";      // Function Call    document.write(bcdToDecimal(s.split('')));       </script>
828
 
Time complexity: O(N) Auxiliary Space complexity: O(1) 
coder001
29AjayKumar
ankthon
arorakashish0911
susmitakundugoaldanga
base-conversion
number-theory
Bit Magic
Mathematical
Strings
number-theory
Strings
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to find whether a given number is power of 2
Little and Big Endian Mystery
Bits manipulation (Important tactics)
Binary representation of a given number
Josephus problem | Set 1 (A O(n) Solution)
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays