ADVERTISEMENT
ADVERTISEMENT

Java program to calculate the product of two binary numbers

Given two binary numbers, we have to calculate the product of two binary numbers.
Submitted by Nidhi, on March 01, 2022

Problem Solution:

In this program, we will read two integer numbers in binary format. Then we will calculate the product of input numbers and print the result.

Program/Source Code:

The source code to calculate the product of two binary numbers is given below. The given program is compiled and executed successfully.

// Java program to calculate the product 
// of two binary numbers

import java.util.Scanner;

public class Main {
  static long binaryProduct(long binNum1, long binNum2) {
    int i = 0;
    long rem = 0;
    long product = 0;
    long sum[] = new long[20];

    while (binNum1 != 0 || binNum2 != 0) {
      sum[i] = (binNum1 % 10 + binNum2 % 10 + rem) % 2;
      rem = (binNum1 % 10 + binNum2 % 10 + rem) / 2;

      binNum1 = binNum1 / 10;
      binNum2 = binNum2 / 10;
      i = i + 1;
    }

    if (rem != 0)
      sum[i] = rem;

    while (i >= 0) {
      product = product * 10 + sum[i];
      i = i - 1;
    }

    return product;
  }

  public static void main(String[] args) {
    Scanner SC = new Scanner(System.in);

    long binNum1 = 0;
    long binNum2 = 0;
    long product = 0;

    long digit = 0;
    long factor = 1;

    System.out.printf("Enter Number1: ");
    binNum1 = SC.nextLong();

    System.out.printf("Enter Number2: ");
    binNum2 = SC.nextLong();

    while (binNum2 != 0) {
      digit = (long)(binNum2 % 10L);

      if (digit == 1) {
        binNum1 = binNum1 * factor;
        product = binaryProduct(binNum1, product);
      } else {
        binNum1 = binNum1 * factor;
      }

      binNum2 = binNum2 / 10;
      factor = 10;
    }
    System.out.println("Product of numbers: " + product);
  }
}

Output:

Enter Number1: 1010
Enter Number2: 1011
Product of numbers: 1101110

Explanation:

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains two static methods binaryProduct() and main().

The binaryProduct() method is used to multiply two binary numbers and return the result to the calling method.

The main() method is an entry point for the program. Here, we read two integer numbers in binary format and printed the resultscreen.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.