Java program for addition of two binary numbers.



package demo;
import java.util.Scanner;
public class BinaryAddition 
{
 public static void main(String[] args)
 {
  long binary1, binary2;
  int i = 0, remainder = 0;
  int[] sum = new int[20];
  Scanner sc = new Scanner(System.in);

  System.out.print("Enter any first binary number: ");
  binary1 = sc.nextLong();
  System.out.print("Enter any second binary number: ");
  binary2 = sc.nextLong();

  while (binary1 != 0 || binary2 != 0) 
  {
   sum[i++] = (int)((binary1 % 10 + binary2 % 10 + remainder) % 2);
   remainder = (int)((binary1 % 10 + binary2 % 10 + remainder) / 2);
   binary1 = binary1 / 10;
   binary2 = binary2 / 10;
  }
  if (remainder != 0) {
   sum[i++] = remainder;
  }
  --i;
  System.out.print("Sum of two binary numbers: ");
  while (i >= 0) {
   System.out.print(sum[i--]);
  }
 }
}

Output:
Enter any first binary number: 111111
Enter any second binary number: 101010
Sum of two binary numbers: 1101001
BUILD SUCCESSFUL (total time: 8 seconds)


No comments:

Post a Comment