0% found this document useful (0 votes)
2 views2 pages

Early Computing 4

Early Computing 4

Uploaded by

Reden Tomé
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Early Computing 4

Early Computing 4

Uploaded by

Reden Tomé
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

ELEBIYO TOME, RUBEN. BSIT.

10/05/24

SANTOS, OLYN DARIANE

Source Code:

Import java.util.Scanner;

// opens the class and declares the main method and scanner

Public class DecimalToBinaryCalculator {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Starts asking for the decimal number unless the word “STOP”
is entered

For (;;) {

System.out.print(“Please enter a decimal number (insert


‘STOP’ to exit): “);

String userInput = scanner.nextLine();

If (userInput.equalsIgnoreCase(“STOP”)) {

System.out.println(“Program terminated, thanks for using


^.^”);

Break; // Exit the loop

//excecutes the code that can have an exception, in that


case: e

Try {

Int decimalNumber = Integer.parseInt(userInput);

String binaryNumber =
Integer.toBinaryString(decimalNumber); // Converts the decimal
number to binary

System.out.println(“Decimal: “ + decimalNumber + “ =>


Binary: “ + binaryNumber);
// if we have an exception, that will handle it correctly

} catch (NumberFormatException e) {

System.out.println(“Invalid input! Please enter a valid


decimal number or ‘STOP’.”);

// Closes the scanner

Scanner.close();

//end of the class

PROOF SCREENSHOTS:

You might also like