Related Articles

Related Articles

Convert a given Decimal number to its BCD representation
  • Last Updated : 21 Dec, 2020

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: 
 

  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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to convert Decimal to BCD
def 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 Number
N = 12
 
# Function Call
BCDConversion(N)
 
# This code is contributed by divyeshrabadiya07

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using 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

chevron_right


Output: 

0001 0010

 

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :