Answers for "Write a Java program that performs binary addition on two binary numbers. Use keyboard input to provide the value for the 2 binary numbers. In BinAddition.java you have been provided with an algorithim to help you solve this question."

1

addition of two binary numbers in java

import java.util.Scanner;
public class JavaExample {
   public static void main(String[] args)
   {	 
	long b1, b2;
	int i = 0, carry = 0;
	int[] sum = new int[10];
	Scanner scanner = new Scanner(System.in);
	System.out.print("Enter first binary number: ");
	b1 = scanner.nextLong();
	System.out.print("Enter second binary number: ");
	b2 = scanner.nextLong();
	scanner.close();
	while (b1 != 0 || b2 != 0) 
	{
		sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);
		carry = (int)((b1 % 10 + b2 % 10 + carry) / 2);
		b1 = b1 / 10;
		b2 = b2 / 10;
	}
	if (carry != 0) {
		sum[i++] = carry;
	}
	--i;
	System.out.print("Output: ");
	while (i >= 0) {
		System.out.print(sum[i--]);
	}
	System.out.print("\n");  
   }
}
Posted by: Guest on August-16-2020
1

addition of two binary numbers in java

import java.util.Scanner;
public class JavaExample {
   public static void main(String[] args)
   {	 
	long b1, b2;
	int i = 0, carry = 0;
	int[] sum = new int[10];
	Scanner scanner = new Scanner(System.in);
	System.out.print("Enter first binary number: ");
	b1 = scanner.nextLong();
	System.out.print("Enter second binary number: ");
	b2 = scanner.nextLong();
	scanner.close();
	while (b1 != 0 || b2 != 0) 
	{
		sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);
		carry = (int)((b1 % 10 + b2 % 10 + carry) / 2);
		b1 = b1 / 10;
		b2 = b2 / 10;
	}
	if (carry != 0) {
		sum[i++] = carry;
	}
	--i;
	System.out.print("Output: ");
	while (i >= 0) {
		System.out.print(sum[i--]);
	}
	System.out.print("\n");  
   }
}
Posted by: Guest on August-16-2020

Code answers related to "Write a Java program that performs binary addition on two binary numbers. Use keyboard input to provide the value for the 2 binary numbers. In BinAddition.java you have been provided with an algorithim to help you solve this question."

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language