📜  将给定的十进制数转换为其BCD表示形式

📅  最后修改于: 2021-05-25 08:55:35             🧑  作者: Mango

给定十进制数N ,任务是将N转换为二进制编码的十进制(BCD)形式。
例子:

方法:

  1. 使用本文讨论的方法反转给定数字N的数字,并将该数字存储在Rev中
  2. 提取Rev的数字并使用位集打印数字的二进制形式。
  3. Rev中的每个数字重复上述步骤。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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;
}


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


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


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


输出:
0001 0010

时间复杂度: O(log 10 N) ,其中N是给定的数字。