package demo;
import java.util.Scanner;
public class BinaryMultiplication
{
public static void main(String[] args)
{
long binary1, binary2, multiply = 0;
int digit, factor = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first binary number: ");
binary1 = sc.nextLong();
System.out.print("Enter the second binary number: ");
binary2 = sc.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);
}
static int binaryproduct(int binary1, int binary2)
{
int i = 0, remainder = 0;
int[] sum = new int[20];
int binaryprod = 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)
{
binaryprod = binaryprod * 10 + sum[i--];
}
return binaryprod;
}
}
Output:
Enter the first binary number: 11100
Enter the second binary number: 11100
Product of two binary numbers: 1100010000
BUILD SUCCESSFUL (total time: 16 seconds)
import java.util.Scanner;
public class BinaryMultiplication
{
public static void main(String[] args)
{
long binary1, binary2, multiply = 0;
int digit, factor = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first binary number: ");
binary1 = sc.nextLong();
System.out.print("Enter the second binary number: ");
binary2 = sc.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);
}
static int binaryproduct(int binary1, int binary2)
{
int i = 0, remainder = 0;
int[] sum = new int[20];
int binaryprod = 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)
{
binaryprod = binaryprod * 10 + sum[i--];
}
return binaryprod;
}
}
Output:
Enter the first binary number: 11100
Enter the second binary number: 11100
Product of two binary numbers: 1100010000
BUILD SUCCESSFUL (total time: 16 seconds)
|
No comments:
Post a Comment