Given a BCD (Binary Coded Decimal) number, the task is to convert the BCD number into its equivalent Decimal number.
Examples:
Input: BCD = 100000101000
Output: 828
Explanation:
Dividing the number into chunks of 4, it becomes 1000 0010 1000.
Here, 1000 is equivalent to 8 and
0010 is equivalent to 2.
So, the number becomes 828.
Input: BCD = 1001000
Output: 48
Explanation:
Dividing the number into chunks of 4, it becomes 0100 1000.
Here, 0100 is equivalent to 4 and
1000 is equivalent to 8.
So, the number becomes 48.
Approach:
- Iterate over all bits in given BCD numbers.
- Divide the given BCD number into chunks of 4, and start computing its equivalent Decimal number.
- Store this number formed in a variable named sum.
- Start framing a number from the digits stored in the sum in a variable num.
- Reverse the number formed so far and return that number.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int bcdToDecimal(string s)
{
int len = s.length(),
check = 0, check0 = 0;
int num = 0, sum = 0,
mul = 1, rev = 0;
for ( int i = len - 1; i >= 0; i--) {
sum += (s[i] - '0' ) * mul;
mul *= 2;
check++;
if (check == 4 || i == 0) {
if (sum == 0 && check0 == 0) {
num = 1;
check0 = 1;
}
else {
num = num * 10 + sum;
}
check = 0;
sum = 0;
mul = 1;
}
}
while (num > 0) {
rev = rev * 10 + (num % 10);
num /= 10;
}
if (check0 == 1)
return rev - 1;
return rev;
}
int main()
{
string s = "100000101000" ;
cout << bcdToDecimal(s);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int bcdToDecimal(String s)
{
int len = s.length();
int check = 0 , check0 = 0 ;
int num = 0 , sum = 0 ;
int mul = 1 , rev = 0 ;
for ( int i = len - 1 ; i >= 0 ; i--)
{
sum += (s.charAt(i) - '0' ) * mul;
mul *= 2 ;
check++;
if (check == 4 || i == 0 )
{
if (sum == 0 && check0 == 0 )
{
num = 1 ;
check0 = 1 ;
}
else
{
num = num * 10 + sum;
}
check = 0 ;
sum = 0 ;
mul = 1 ;
}
}
while (num > 0 )
{
rev = rev * 10 + (num % 10 );
num /= 10 ;
}
if (check0 == 1 )
return rev - 1 ;
return rev;
}
public static void main(String[] args)
{
String s = "100000101000" ;
System.out.println(bcdToDecimal(s));
}
}
|
Python3
def bcdToDecimal(s):
length = len (s);
check = 0 ;
check0 = 0 ;
num = 0 ;
sum = 0 ;
mul = 1 ;
rev = 0 ;
for i in range (length - 1 , - 1 , - 1 ):
sum + = ( ord (s[i]) - ord ( '0' )) * mul;
mul * = 2 ;
check + = 1 ;
if (check = = 4 or i = = 0 ):
if ( sum = = 0 and check0 = = 0 ):
num = 1 ;
check0 = 1 ;
else :
num = num * 10 + sum ;
check = 0 ;
sum = 0 ;
mul = 1 ;
while (num > 0 ):
rev = rev * 10 + (num % 10 );
num / / = 10 ;
if (check0 = = 1 ):
return rev - 1 ;
return rev;
if __name__ = = "__main__" :
s = "100000101000" ;
print (bcdToDecimal(s));
|
C#
using System;
class GFG
{
public static int bcdToDecimal(String s)
{
int len = s.Length;
int check = 0, check0 = 0;
int num = 0, sum = 0;
int mul = 1, rev = 0;
for ( int i = len - 1; i >= 0; i--)
{
sum += (s[i] - '0' ) * mul;
mul *= 2;
check++;
if (check == 4 || i == 0)
{
if (sum == 0 && check0 == 0)
{
num = 1;
check0 = 1;
}
else
{
num = num * 10 + sum;
}
check = 0;
sum = 0;
mul = 1;
}
}
while (num > 0)
{
rev = rev * 10 + (num % 10);
num /= 10;
}
if (check0 == 1)
return rev - 1;
return rev;
}
public static void Main(String[] args)
{
String s = "100000101000" ;
Console.WriteLine(bcdToDecimal(s));
}
}
|
Javascript
<script>
function bcdToDecimal(s)
{
let len = s.length;
let check = 0, check0 = 0;
let num = 0, sum = 0;
let mul = 1, rev = 0;
for (let i = len - 1; i >= 0; i--)
{
sum += (s[i] - '0' ) * mul;
mul *= 2;
check++;
if (check == 4 || i == 0)
{
if (sum == 0 && check0 == 0)
{
num = 1;
check0 = 1;
}
else
{
num = num * 10 + sum;
}
check = 0;
sum = 0;
mul = 1;
}
}
while (num > 0)
{
rev = rev * 10 + (num % 10);
num = Math.floor(num / 10);
}
if (check0 == 1)
return rev - 1;
return rev;
}
let s = "100000101000" ;
document.write(bcdToDecimal(s.split( '' )));
</script>
|
Time complexity: O(N)
Auxiliary Space complexity: O(1)
Method: Using bit manipulation method
The bcdToDecimal function takes a string s representing the BCD number as input and returns its decimal equivalent. It first initializes the num variable to 0. Then, it iterates through the bits of the BCD string in groups of 4, using the substr function to extract each group. For each group, it creates a bitset object with the 4 bits, and converts it to decimal using the to_ulong function. It then multiplies the decimal value by the appropriate place value and adds it to the num variable. Finally, the function returns num.
C++
#include <iostream>
#include <bitset>
using namespace std;
int bcdToDecimal(string s)
{
int len = s.length(), num = 0;
for ( int i = 0; i < len; i+=4) {
bitset<4> bcd_bits(s.substr(i, 4));
int dec = bcd_bits.to_ulong();
num = num*10 + dec;
}
return num;
}
int main()
{
string s = "100000101000" ;
cout << bcdToDecimal(s);
return 0;
}
|
Java
public class BCDToDecimal {
public static int bcdToDecimal(String s) {
int len = s.length();
int num = 0 ;
for ( int i = 0 ; i < len; i += 4 ) {
String bcdBits = s.substring(i, i + 4 );
int dec = Integer.parseInt(bcdBits, 2 );
num = num * 10 + dec;
}
return num;
}
public static void main(String[] args) {
String s = "100000101000" ;
System.out.println(bcdToDecimal(s));
}
}
|
Python3
def bcd_to_decimal(s):
num = 0
len_s = len (s)
for i in range ( 0 , len_s, 4 ):
bcd_bits = s[i:i + 4 ]
dec = int (bcd_bits, 2 )
num = num * 10 + dec
return num
s = "100000101000"
print (bcd_to_decimal(s))
|
C#
using System;
public class GFG
{
public static int BcdToDecimal( string s)
{
int len = s.Length;
int num = 0;
for ( int i = 0; i < len; i += 4)
{
string bcdSubstring = s.Substring(i, 4);
int dec = Convert.ToInt32(bcdSubstring, 2);
num = num * 10 + dec;
}
return num;
}
public static void Main()
{
string s = "100000101000" ;
Console.WriteLine(BcdToDecimal(s));
}
}
|
Javascript
function bcdToDecimal(s) {
const len = s.length;
let num = 0;
for (let i = 0; i < len; i += 4) {
const bcdSubstring = s.substring(i, i + 4);
const dec = parseInt(bcdSubstring, 2);
num = num * 10 + dec;
}
return num;
}
const s = "100000101000" ;
console.log(bcdToDecimal(s));
|
The time complexity of this algorithm is O(n), where n is the length of the BCD string. This is because the algorithm must iterate through each bit of the string once.
The auxiliary space complexity is also O(n), because the bitset objects created in each iteration require O(1) space, but there are n/4 iterations.