Convert a given Decimal number to its BCD representation

Given a decimal number N, the task is to convert N to it’s Binary Coded Decimal(BCD) form.


Input: N = 12
Output: 0001 0000
Considering 4-bit concept:
1 in binary is 0001 and 2 in binary is 0010.
So it’s equivalent BCD is 0001 0010.

Input: N = 10
Output: 0001 0000
Considering 4-bit concept:
1 in binary is 0001 and 0 in binary is 0000.
So it’s equivalent BCD is 0001 0000.


  1. Reverse the digits of the given number N using the approach discussed in this article and stored the number in Rev.
  2. Extract the digits of Rev and print the Binary form of the digit using bitset.
  3. Repeat the above steps for each digit in Rev.

Below is the implementation of the above approach:





// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to convert Decimal to BCD
void BCDConversion(int n)
    // Base Case
    if (n == 0) {
        cout << "0000";
    // To store the reverse of n
    int rev = 0;
    // Reversing the digits
    while (n > 0) {
        rev = rev * 10 + (n % 10);
        n /= 10;
    // Iterate through all digits in rev
    while (rev > 0) {
        // Find Binary for each digit
        // using bitset
        bitset<4> b(rev % 10);
        // Print the Binary conversion
        // for current digit
        cout << b << ' ';
        // Divide rev by 10 for next digit
        rev /= 10;
// Driver Code
int main()
    // Given Number
    int N = 12;
    // Function Call
    return 0;



0001 0010

Time Complexity: O(log10 N), where N is the given number.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.