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 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.
Below is the implementation of the above approach:
C++
// 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
// 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
# 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#
// 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 |
Javascript
<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.
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


