Question

In: Computer Science

use java Write a program that, given two binary numbers represented as strings, prints their sum...

use java
Write a program that, given two binary numbers represented as strings, prints their sum in binary. The binary strings are comma separated, two per line. The final answer should not have any leading zeroes. In case the answer is zero, just print one zero i.e. 0

Input:

Your program should read lines from standard input. Each line contains two binary strings, separated by a comma and no spaces.

Output:

For each pair of binary numbers print to standard output their binary sum, one per line.

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.



import java.util.Scanner;

public class Addition
{
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String[] numbers=scanner.nextLine().split(",");
        while(numbers[0].length()>0) // if the user enters empty line, break loop
        {
            //call the function
            System.out.println(numbers[0]+" + "+numbers[1]+" = "+add(numbers[0],numbers[1]));
            numbers=scanner.nextLine().split(",");
        }
        System.out.println("Good Bye!!");
    }

    private static String add(String a, String b) {
        String answer="";
        int sum=0;
        int i= a.length()-1;
        int j=b.length()-1;
        while (i >= 0 || j >= 0 || sum == 1)
        {
            // Compute sum of last digits and carry
            sum += ((i >= 0)? a.charAt(i) - '0': 0);
            sum += ((j >= 0)? b.charAt(j) - '0': 0);

            // If current digit sum is
            // 1 or 3, add 1 to result
            answer = (char)(sum % 2 + '0') + answer;
            // Compute carry
            sum /= 2;
            // Move to next digits
            i--;
            j--;
        }
        // remove leading zeroes
        String finalAnswer="";
        for(int k=0;k<answer.length();k++)
            if(answer.charAt(k)=='1')
            {
                finalAnswer=answer.substring(k);
                break;
            }
        // check for answer= 0
        if(finalAnswer=="")
            finalAnswer="0";

        return finalAnswer;
    }
}

=====================

SCREENSHOT:

OUTPUT


Related Solutions

in java Write an application that gets two numbers from the user and prints the sum,...
in java Write an application that gets two numbers from the user and prints the sum, product, difference and quotient of the two numbers in a GUI.
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For example, java Adder 3 2.5 -4.1 should print The sum is 1.4
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
​Write a program which requests two weights in kilograms and grams and prints the sum of...
​Write a program which requests two weights in kilograms and grams and prints the sum of the weights. For example, if the weights are 3kg 500g and 4kg 700g, your program should print 8kg 200g. in c programming selection logic
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
write a program to find the maximum possible sum such that no two chosen numbers are...
write a program to find the maximum possible sum such that no two chosen numbers are adjacent either vertically, horizontally, or diagonally. code in java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT