BCD addition of given Decimal numbers
Given two numbers A and B, the task is to perform BCD Addition of the given numbers.
Examples:
Input: A = 12, B = 20
Output: 110010
Explanation:
The summation of A and B is 12 + 20 = 32.
The binary representation of 3 = 0011
The binary representation of 2 = 0010
Therefore, the BCD Addition is “0011” + “0010” = “110010”
Input: A = 10, B = 10
Output:100000
Explanation:
The summation of A and B is 10 + 10 = 20.
The binary representation of 2 = 0010
The binary representation of 0 = 0000
Therefore, the BCD Addition is “0010” + “0000” = “100000”
Approach: The idea is to convert the summation of given two numbers A and B to BCD Number. Below are the steps:
- Find the summation(say num) of the two given numbers A and B.
- For each digit in the number num, convert it into binary representation up to 4 bits.
- Concatenate the binary representation of each digit above and print the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string BCDAddition( int A, int B)
{
string s = to_string(A + B);
int l = s.length();
string ans;
string str;
for ( int i = 0; i < l; i++) {
str = bitset<4>(s[i]).to_string();
ans.append(str);
}
const auto loc1 = ans.find( '1' );
if (loc1 != string::npos) {
return ans.substr(loc1);
}
return "0" ;
}
int main()
{
int A = 12, B = 20;
cout << BCDAddition(A, B);
return 0;
}
|
Java
class GFG{
static String BCDAddition( int A, int B)
{
String s = String.valueOf(A + B);
int l = s.length();
String temp[] = { "0000" , "0001" ,
"0010" , "0011" ,
"0100" , "0101" ,
"0110" , "0111" ,
"1000" , "1001" };
String ans = "" ;
for ( int i = 0 ; i < l; i++)
{
String t = temp[s.charAt(i) - '0' ];
ans = ans + String.valueOf(t);
}
int loc1 = 0 ;
while (loc1 < l && ans.charAt(loc1) != '1' )
{
loc1++;
}
return ans.substring(loc1);
}
public static void main(String[] args)
{
int A = 12 ;
int B = 20 ;
System.out.println(BCDAddition(A, B));
}
}
|
Python3
def BCDAddition(A, B):
s = str (A + B)
l = len (s)
temp = [ "0000" , "0001" , "0010" , "0011" , "0100" ,
"0101" , "0110" , "0111" , "1000" , "1001" ]
ans = ""
for i in range (l):
t = temp[ ord (s[i]) - ord ( '0' )]
ans = ans + str (t)
loc1 = ans.find( '1' )
return ans[loc1:]
A = 12
B = 20
print (BCDAddition(A, B))
|
C#
using System;
class GFG
{
static String BCDAddition( int A, int B)
{
string s = (A + B).ToString();
int l = s.Length;
string [] temp = { "0000" , "0001" ,
"0010" , "0011" ,
"0100" , "0101" ,
"0110" , "0111" ,
"1000" , "1001" };
string ans = "" ;
for ( int i = 0; i < l; i++)
{
string t = temp[s[i] - '0' ];
ans = ans + t.ToString();
}
int loc1 = 0;
while (loc1 < l && ans[loc1] != '1' )
{
loc1++;
}
return ans.Substring(loc1);
}
static void Main()
{
int A = 12;
int B = 20;
Console.Write(BCDAddition(A, B));
}
}
|
Javascript
<script>
function BCDAddition(A, B)
{
var s = (A + B).toString();
var l = s.length;
temp = [ "0000" , "0001" ,
"0010" , "0011" ,
"0100" , "0101" ,
"0110" , "0111" ,
"1000" , "1001" ]
var ans = "" ;
for ( var i = 0; i < l; i++)
{
var t = temp[s[i] - '0' ];
ans = ans + t.toString();
}
var loc1 = 0;
while (loc1 < l && ans[loc1] != '1' )
{
loc1++;
}
return ans.substring(loc1);
}
var A = 12;
var B = 20;
document.write(BCDAddition(A, B));
</script>
|
Time Complexity: O(log10(A+B))
Last Updated :
07 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...