Related Articles

# BCD addition of given Decimal numbers

• Difficulty Level : Easy
• Last Updated : 26 Mar, 2021

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:

1. Find the summation(say num) of the two given numbers A and B.
2. For each digit in the number num, convert it into binary representation up to 4 bits.
3. Concatenate the binary representation of each digit above and print the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to perform BCD Addition``string BCDAddition(``int` `A, ``int` `B)``{` `    ``// Store the summation of A and B``    ``// in form of string``    ``string s = to_string(A + B);``    ``int` `l = s.length();` `    ``// To store the final result``    ``string ans;` `    ``string str;` `    ``// Forming BCD using Bitset``    ``for` `(``int` `i = 0; i < l; i++) {` `        ``// Find the binary representation``        ``// of the current characters``        ``str = bitset<4>(s[i]).to_string();``        ``ans.append(str);``    ``}` `    ``// Stripping off leading zeroes.``    ``const` `auto` `loc1 = ans.find(``'1'``);` `    ``// Return string ans``    ``if` `(loc1 != string::npos) {``        ``return` `ans.substr(loc1);``    ``}``    ``return` `"0"``;``}` `// Driver Code``int` `main()``{``    ``// Given Numbers``    ``int` `A = 12, B = 20;` `    ``// Function Call``    ``cout << BCDAddition(A, B);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG{``    ` `// Function to perform BCD Addition``static` `String BCDAddition(``int` `A, ``int` `B)``{``    ` `    ``// Store the summation of A and B``    ``// in form of string``    ``String s = String.valueOf(A + B);``    ``int` `l = s.length();`` ` `    ``// Forming BCD using Bitset``    ``String temp[] = { ``"0000"``, ``"0001"``,``                      ``"0010"``, ``"0011"``,``                      ``"0100"``, ``"0101"``,``                      ``"0110"``, ``"0111"``,``                      ``"1000"``, ``"1001"` `};``    ``String ans = ``""``;``     ` `    ``for``(``int` `i = ``0``; i < l; i++)``    ``{``        ` `        ``// Find the binary representation``        ``// of the current characters``        ``String t = temp[s.charAt(i) - ``'0'``];``        ``ans = ans + String.valueOf(t);``    ``}``         ` `    ``// Stripping off leading zeroes.``    ``int` `loc1 = ``0``;``    ``while` `(loc1 < l && ans.charAt(loc1) != ``'1'``)``    ``{``        ``loc1++;``    ``}`` ` `    ``// Return string ans``    ``return` `ans.substring(loc1);``}` `// Driver code    ``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given Numbers``    ``int` `A = ``12``;``    ``int` `B = ``20``;``     ` `    ``// Function Call``    ``System.out.println(BCDAddition(A, B));``}``}` `// This code is contributed by divyesh072019`

## Python3

 `# Python3 program for the above approach` `# Function to perform BCD Addition``def` `BCDAddition(A, B):` `    ``# Store the summation of A and B``    ``# in form of string``    ``s ``=` `str``(A ``+` `B)``    ``l ``=` `len``(s)` `    ``# Forming BCD using Bitset``    ``temp ``=` `[ ``"0000"``, ``"0001"``, ``"0010"``, ``"0011"``, ``"0100"``,``             ``"0101"``, ``"0110"``, ``"0111"``, ``"1000"``, ``"1001"` `]``    ``ans ``=` `""``    ` `    ``for` `i ``in` `range``(l):` `        ``# Find the binary representation``        ``# of the current characters``        ``t ``=` `temp[``ord``(s[i]) ``-` `ord``(``'0'``)]``        ``ans ``=` `ans ``+` `str``(t)``        ` `    ``# Stripping off leading zeroes.``    ``loc1 ``=` `ans.find(``'1'``)` `    ``# Return string ans``    ``return` `ans[loc1:]` `# Driver Code` `# Given Numbers``A ``=` `12``B ``=` `20` `# Function Call``print``(BCDAddition(A, B))` `# This code is contributed by grand_master`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG``{``    ` `    ``// Function to perform BCD Addition``    ``static` `String BCDAddition(``int` `A, ``int` `B)``    ``{``         ` `        ``// Store the summation of A and B``        ``// in form of string``        ``string` `s = (A + B).ToString();``        ``int` `l = s.Length;``      ` `        ``// Forming BCD using Bitset``        ``string``[] temp = { ``"0000"``, ``"0001"``,``                          ``"0010"``, ``"0011"``,``                          ``"0100"``, ``"0101"``,``                          ``"0110"``, ``"0111"``,``                          ``"1000"``, ``"1001"` `};``        ``string` `ans = ``""``;         ``        ``for``(``int` `i = 0; i < l; i++)``        ``{``             ` `            ``// Find the binary representation``            ``// of the current characters``            ``string` `t = temp[s[i] - ``'0'``];``            ``ans = ans + t.ToString();``        ``}``              ` `        ``// Stripping off leading zeroes.``        ``int` `loc1 = 0;``        ``while` `(loc1 < l && ans[loc1] != ``'1'``)``        ``{``            ``loc1++;``        ``}``      ` `        ``// Return string ans``        ``return` `ans.Substring(loc1);``    ``}` `  ``// Driver code``  ``static` `void` `Main()``  ``{``    ` `    ``// Given Numbers``    ``int` `A = 12;``    ``int` `B = 20;``      ` `    ``// Function Call``    ``Console.Write(BCDAddition(A, B));``  ``}``}` `// This code is contributed by divyeshrbadiya07.`

## Javascript

 ``
Output:
`110010`

Time Complexity: O(log10(A+B))

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up