Posted on by Kalkicode
Code Bit Logic

BCD addition of two decimal numbers

The problem we're addressing involves performing Binary Coded Decimal (BCD) addition of two decimal numbers. BCD is a way of representing decimal digits using a binary system, where each decimal digit is represented by its binary equivalent. The task is to create a program that can perform BCD addition on two decimal numbers and provide the result in BCD form.

Example BCD addition of given decimal numbers

Problem Statement and Description

Given two decimal numbers, we want to perform BCD addition on them. BCD addition involves adding the two numbers in a way that respects the BCD representation of each digit. This means that the addition should be performed digit by digit, and if the result of adding two digits exceeds 9, the carry should be propagated to the next higher-order digit.

Example

Let's consider the numbers 12 and 6. In BCD form, 12 is represented as 0001 0010, and 6 is represented as 0000 0110. When we add these numbers, we get 18 in regular decimal form. In BCD addition, 8 is represented as 1000, and 1 is represented as 0001. The result is 0001 1000, which is the BCD representation of 18.

Idea to Solve the Problem

The key idea to solve this problem is to perform addition digit by digit while considering carry values for each addition operation. We need to implement a mechanism to convert decimal digits to their corresponding 4-bit binary representations (BCD form) and also handle carry propagation.

Pseudocode

Here's the pseudocode for the algorithm:

function getValue(num):
    if num >= 0 and num <= 9:
        return (char)(num + '0')
    else:
        return (char)(num - 10 + 'A')

function binaryValue(num, extra):
    result = ""
    n = num
    while n > 0:
        result = getValue(n % 2) + result
        n /= 2
    if extra is true:
        n = length of result
        while n < 4:
            result = "0" + result
            n++
    return result

function bcdAddition(x, y):
    sum = x + y
    if sum < 0:
        return
    ans = ""
    while sum > 9:
        ans = binaryValue(sum % 10, true) + ans
        sum /= 10
    ans = binaryValue(sum % 10, false) + ans
    print "(", x, " + ", y, "): ", ans

Algorithm Explanation

  1. getValue(num) converts a decimal digit to its corresponding ASCII character representation.
  2. binaryValue(num, extra) converts a decimal digit to its BCD binary representation. If extra is true, the function adds leading zeros to ensure the output is a 4-bit binary representation.
  3. bcdAddition(x, y) performs BCD addition of two numbers. It iterates through the digits of the sum and converts each digit to its BCD representation.

Code Solution

Resultant Output Explanation

  1. For the numbers 12 and 6:

    • BCD addition: 18
    • BCD representation of 18: 0001 1000
    • The output is "Given num: 18".
  2. For the numbers 7 and 18:

    • BCD addition: 25
    • BCD representation of 25: 0010 0101
    • The output is "Given num: 25".
  3. For the numbers 45 and 32:

    • BCD addition: 77
    • BCD representation of 77: 0111 0111
    • The output is "Given num: 77".
  4. For the numbers 111 and 132:

    • BCD addition: 243
    • BCD representation of 243: 0010 0100 0011
    • The output is "Given num: 243".

Time Complexity

The time complexity of the algorithm primarily depends on the addition and loop operations. Since these operations are performed sequentially and depend on the number of digits in the input numbers, the time complexity can be approximated as O(N), where N is the number of digits in the larger input number.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment