Java
Java

 

Introduction

In computer science, number systems play a crucial role in understanding how computers process information. The most commonly used number systems are decimal (base 10) and binary (base 2).
This program demonstrates how to convert a decimal number (base 10) to its binary representation (base 2) using the Java programming language.

Objective

The objective of this program is to take a decimal number as input from the user and convert it to binary. This is done by repeatedly dividing the decimal number by 2, recording the remainders, and then reversing the remainders to form the binary representation.

Java Code


public class DecimalToBinary {

    public static void main(String[] args) {
        // Create an instance of Scanner to read user input
        java.util.Scanner scanner = new java.util.Scanner(System.in);
        
        // Prompt the user to enter a decimal number
        System.out.print("Enter a decimal number: ");
        int decimal = scanner.nextInt();
        
        // Initialize an empty string to store the binary representation
        String binary = "";
        
        // Convert the decimal number to binary
        while (decimal > 0) {
            binary = (decimal % 2) + binary;  // Prepend the remainder (binary digit)
            decimal = decimal / 2;  // Divide the decimal number by 2
        }
        
        // Output the binary representation
        System.out.println("The binary representation is: " + binary);
        
        // Close the scanner to avoid resource leak
        scanner.close();
    }
}
            

Explanation of the Program Structure

The program follows a simple approach to convert a decimal number to binary:

  1. Input: It prompts the user to enter a decimal number using the Scanner class.
  2. Conversion: The program divides the decimal number by 2, appending the remainder (either 0 or 1) to the binary string. This process is repeated until the decimal number is reduced to 0.
  3. Output: Once the conversion is complete, the program outputs the binary string.
  4. Scanner Close: The program ensures that the scanner is closed to avoid any resource leaks.

How to Run the Program

To run this program on your system, follow these steps:

  1. Ensure you have Java Development Kit (JDK) installed on your system. If not, download and install it from the official Java website.
  2. Create a new Java file with the name DecimalToBinary.java and paste the code into the file.
  3. Open a terminal or command prompt and navigate to the directory where the DecimalToBinary.java file is located.
  4. Compile the Java program by running the following command:
    javac DecimalToBinary.java
  5. Run the compiled program with the following command:
    java DecimalToBinary
  6. Enter a decimal number when prompted, and the program will display the corresponding binary number.
© 2024 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)