Convert a given Decimal number to its BCD representation - GeeksforGeeks

29 Jul, 2021
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 is 0001 and 2 in binary is 0010. So it’s equivalent BCD is 0001 0010.Input: N = 10 Output: 0001 0000 Explanation: Considering 4-bit concept: 1 in binary is 0001 and 0 in binary is 0000. 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.
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++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to convert Decimal to BCDvoid 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 Codeint main(){    // Given Number    int N = 12;     // Function Call    BCDConversion(N);    return 0;}
// Java program for the above approachimport java.util.*; class Gfg{       // Function to convert Decimal to BCD    public static void BCDConversion(int n)    {               // Base Case        if(n == 0)        {            System.out.print("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            String b = Integer.toBinaryString(rev % 10);                         b = String.format("%04d", Integer.parseInt(b));                           // Print the Binary conversion            // for current digit            System.out.print(b + " ");                       // Divide rev by 10 for next digit            rev /= 10;        }    }     // Driver code  public static void main(String []args)  {         // Given Number    int N = 12;         // Function Call    BCDConversion(N);  }} // This code is contributed by avanitrachhadiya2155
# Python3 program for the above approach # Function to convert Decimal to BCDdef BCDConversion(n) :     # Base Case    if (n == 0) :        print("0000")        return     # To store the reverse of n    rev = 0     # Reversing the digits    while (n > 0) :        rev = rev * 10 + (n % 10)        n = n // 10     # Iterate through all digits in rev    while (rev > 0) :         # Find Binary for each digit        # using bitset        b = str(rev % 10)                 # Print the Binary conversion        # for current digit        print("{0:04b}".format(int(b, 16)), end = " ")         # Divide rev by 10 for next digit        rev = rev // 10 # Given NumberN = 12 # Function CallBCDConversion(N) # This code is contributed by divyeshrabadiya07
// C# program for the above approachusing System;using System.Collections.Generic;class GFG {         // Function to convert Decimal to BCD    static void BCDConversion(int n)    {        // Base Case        if (n == 0) {            Console.Write("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            string b = Convert.ToString(rev % 10, 2).PadLeft(4, '0');                  // Print the Binary conversion            // for current digit            Console.Write(b + " ");                  // Divide rev by 10 for next digit            rev /= 10;        }    }   static void Main() {           // Given Number    int N = 12;      // Function Call    BCDConversion(N);  }} // This code is contributed divyesh072019
<script>     //JavaScript for the above approach     // Function to convert Decimal to BCD    function BCDConversion(n)    {        // Base Case        if (n == 0) {            document.write("0000");            return;        }         // To store the reverse of n        let rev = 0;         // Reversing the digits        while (n > 0) {            rev = rev * 10 + (n % 10);            n = parseInt(n / 10, 10);        }         // Iterate through all digits in rev        while (rev > 0) {             // Find Binary for each digit            // using bitset            let b = (rev % 10).toString(2);            while(b.length != 4)            {                b = "0" + b;            }             // Print the Binary conversion            // for current digit            document.write(b + " ");             // Divide rev by 10 for next digit            rev = parseInt(rev / 10, 10);        }    }     // Driver Code    // Given Number    let N = 12;     // Function Call    BCDConversion(N); </script>
0001 0010
 
Time Complexity: O(log10 N), where N is the given number. 
divyeshrabadiya07
divyesh072019
avanitrachhadiya2155
vaibhavrabadiya3
base-conversion
Bit Magic
Mathematical
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Cyclic Redundancy Check and Modulo-2 Division
Program to find parity
Bit Fields in C
Little and Big Endian Mystery
Bits manipulation (Important tactics)
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7