# 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.

**Examples:**

Input:N = 12

Output:0001 0000

Explanation:

Considering 4-bit concept:

1 in binary is0001and 2 in binary is0010.

So it’s equivalent BCD is 0001 0010.

Input:N = 10

Output:0001 0000

Explanation:

Considering 4-bit concept:

1 in binary is0001and 0 in binary is0000.

So it’s equivalent BCD is 0001 0000.

**Approach:**

- Reverse the digits of the given number
**N**using the approach discussed in this article and stored the number in**Rev**. - Extract the digits of
**Rev**and print the Binary form of the digit using bitset. - 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"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// 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 ` ` ` `BCDConversion(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

0001 0010

**Time Complexity:** *O(log _{10} 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.

## Recommended Posts:

- Check if Decimal representation of an Octal number is divisible by 7
- Convert a number into negative base representation
- Convert decimal fraction to binary number
- Program to convert a BCD to Hexa-Decimal Number
- Program to convert Hexa-Decimal Number to its equivalent BCD
- Python program to convert decimal to binary number
- Convert Decimal To Hexa-Decimal including negative numbers
- Program to Convert BCD number into Decimal number
- Decimal representation of given binary string is divisible by 20 or not
- Decimal representation of given binary string is divisible by 10 or not
- Convert numbers into binary representation and add them without carry
- Convert Binary fraction to Decimal
- Convert from any base to decimal and vice versa
- Quickly convert Decimal to other bases in Python
- Convert all substrings of length 'k' from base 'b' to decimal
- Count number of trailing zeros in Binary representation of a number using Bitset
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Number of leading zeros in binary representation of a given number
- Count number of digits after decimal on dividing a number
- Binary representation of a given number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.