129k views
22 votes
Write a Java program that prompts for integers and displays them in binary. Sample output: Do you want to start(Y/N): y Enter an integer and I will convert it to binary code: 16 You entered 16. 16 in binary is 10000. Do you want to continue(Y/N): y Enter an integer and I will convert it to binary code: 123 You entered 123. 123 in binary is 1111011. Do you want to continue(Y/N): y Enter an integer and I will convert it to binary code: 359 You entered 359. 359 in binary is 101100111. Do you want to continue(Y/N): y Enter an integer and I will convert it to binary code: 1024 You entered 1024. 1024 in binary is 10000000000. Do you want to continue(Y/N): n

1 Answer

0 votes

Answer:

Step-by-step explanation:

The following code is written in Java and creates a loop that cycles the same prompt until the user states no. While the loop runs it asks the user for an integer value and returns the binary code of that value

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

boolean continueLoop = true;

while (continueLoop) {

System.out.println("Would you like to continue? Y/N");

String answer = in.nextLine().toLowerCase();

if (answer.equals("y")) {

System.out.println("Enter int value: ");

int number = in.nextInt();

System.out.println("Integer: "+number);

System.out.println("Binary = " + Integer.toBinaryString(number));

} else if (answer.equals("n")){

System.out.println("breaking");

break;

}

}

}

User Tom Walters
by
4.6k points