Write a Java program to add two binary numbers.
Question
Write a Java program to add two binary numbers.
Solution
Sure, here is a simple Java program that adds two binary numbers:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first binary number: ");
String binary1 = scanner.nextLine();
System.out.println("Enter the second binary number: ");
String binary2 = scanner.nextLine();
int num1 = Integer.parseInt(binary1, 2);
int num2 = Integer.parseInt(binary2, 2);
int sum = num1 + num2;
System.out.println("The sum of the two binary numbers is: " + Integer.toBinaryString(sum));
}
}
Here's how this program works:
-
We first import the Scanner class, which is used to get user input.
-
In the main method, we create an instance of Scanner.
-
We ask the user to enter the first and second binary numbers.
-
We use the
Integer.parseInt()method to convert the binary numbers to decimal (base 10). -
We add the two decimal numbers together.
-
We convert the sum back to binary using the
Integer.toBinaryString()method. -
Finally, we print out the sum of the two binary numbers.
Similar Questions
Sum of binary numbersImplement a Java program that takes two binary numbers as input, performs addition on them, and outputs in binaryConstraints:NAExample:Input:10100101Output:1111Explanation:NA
Write a program to add two numbers using pointers
Add two numbersYou have been provided with some code. The code should take in two numbers from the user, add them together, and then display the result to the user. However, the code has bugs!Debug this code!Here are some examples of how the program should work:Example 1Enter a number: 32Enter another number: 64The sum of your two numbers is: 96.00Example 2Enter a number: 1234.5678Enter another number: 9999.99The sum of your two numbers is: 11234.56Example 3Enter a number: 23Enter another number: -532.824The sum of your two numbers is: -509.82
Java program that reads an integer between 0 and 1000 and adds all the digits in the integerWrite a Java program that reads an integer between 0 and 1000 and adds all the digits in the integer.An integer is a number that can be written without a fractional component. For example, 23, 6, 0, and −1245 are integers, while 3.25, 7 1⁄2, and √3 are not. Click on the image for preview.Constraints:N/AExample:Inputan integer between 0 and 1000: 565Output:The sum of all digits in 565 is 16Explanation:-Public Test Cases:# INPUT EXPECTED OUTPUT1 56516
Thara is attending a math lecture, and her professor asks students to swap two numbers using a bitwise operator. The professor finds it difficult to check the logic of 50 students. So, he decided to write a Java program to get two numbers, first and second, from the user and swap the numbers. He is not good at programming. Help them by developing an application using your Java skills.Note:Use bitwise operator.Negative and zero are also accepted as inputs and numbers have to be swapped accordingly.In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input 1:Enter the numbers8743Sample Output 1:Before swappingfirst = 87, second = 43After swappingfirst = 43, second = 87
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.