1. Introduction
In this tutorial, we are going to write a Java program that converts a decimal number to its binary equivalent. This conversion is foundational in computer science, as binary representation is fundamental to digital systems.
2. Program Steps
1. Define a class named DecimalToBinary.
2. Inside the class, define the main method.
3. Inside the main method, declare an int variable to represent the decimal number.
4. Use the Integer.toBinaryString method to convert the decimal number to binary.
5. Print out the decimal number and its binary equivalent.
3. Code Program
public class DecimalToBinary { // Step 1: Define a class named DecimalToBinary
public static void main(String[] args) { // Step 2: Define the main method
// Step 3: Declare an int variable to represent the decimal number
int decimal = 53;
// Step 4: Use Integer.toBinaryString to convert decimal to binary
String binary = Integer.toBinaryString(decimal);
// Step 5: Print the decimal number and its binary equivalent
System.out.println("Decimal Number: " + decimal);
System.out.println("Binary Equivalent: " + binary);
}
}
Output:
Decimal Number: 53 Binary Equivalent: 110101
4. Step By Step Explanation
– Step 1: A class named DecimalToBinary is defined.
– Step 2: The main method is defined within the DecimalToBinary class.
– Step 3: An int variable decimal is declared to represent the decimal number.
– Step 4: The Integer.toBinaryString method is used to convert the decimal number to a binary String.
– Step 5: The original decimal number and its binary equivalent are printed to the console.