Write a Java program to multiply two binary numbers
Input Data:Input the first binary number: 10Input the second binary number: 11Expected Output
Product of two binary numbers: 110
import java.util.Scanner; public class Exercise18 { public static void main(String[] args) { long binary1, binary2, multiply = 0; int digit, factor = 1; Scanner in = new Scanner(System.in); System.out.print("Input the first binary number: "); binary1 = in.nextLong(); System.out.print("Input the second binary number: "); binary2 = in.nextLong(); while (binary2 != 0) { digit = (int)(binary2 % 10); if (digit == 1) { binary1 = binary1 * factor; multiply = binaryproduct((int) binary1, (int) multiply); } else { binary1 = binary1 * factor; } binary2 = binary2 / 10; factor = 10; } System.out.print("Product of two binary numbers: " + multiply+"\n"); } static int binaryproduct(int binary1, int binary2) { int i = 0, remainder = 0; int[] sum = new int[20]; int binary_prod_result = 0; while (binary1 != 0 || binary2 != 0) { sum[i++] = (binary1 % 10 + binary2 % 10 + remainder) % 2; remainder = (binary1 % 10 + binary2 % 10 + remainder) / 2; binary1 = binary1 / 10; binary2 = binary2 / 10; } if (remainder != 0) { sum[i++] = remainder; } --i; while (i >= 0) { binary_prod_result = binary_prod_result * 10 + sum[i--]; } return binary_prod_result; } }
Sample Output:
Input the first binary number: 110 Input the second binary number: 101 Product of two binary numbers: 11110
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer