Java Program to Convert Decimal to Binary

Introduction

Converting a decimal number to binary is a common problem in computer science and digital electronics. Binary numbers are base-2 numbers, consisting only of 0s and 1s, and are fundamental to computing systems. This guide will walk you through writing a Java program that converts a given decimal number to its binary equivalent.

Problem Statement

Create a Java program that:

  • Prompts the user to enter a decimal number.
  • Converts the decimal number to binary.
  • Displays the binary equivalent.

Example:

  • Input: 10

  • Output: "Binary of 10 is 1010"

  • Input: 15

  • Output: "Binary of 15 is 1111"

Solution Steps

  1. Read the Decimal Number: Use the Scanner class to take a decimal number as input from the user.
  2. Convert Decimal to Binary: Use either the built-in method Integer.toBinaryString() or manually convert using a loop.
  3. Display the Result: Print the binary equivalent.

Java Program Using Built-in Method

// Java Program to Convert Decimal to Binary using Integer.toBinaryString()
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class DecimalToBinary {
    public static void main(String[] args) {
        // Step 1: Read the decimal number from the user
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter a decimal number: ");
            int decimal = scanner.nextInt();
            
            // Step 2: Convert the decimal number to binary using built-in method
            String binary = Integer.toBinaryString(decimal);
            
            // Step 3: Display the result
            System.out.println("Binary of " + decimal + " is " + binary);
        }
    }
}

Java Program Using a Manual Conversion Method

// Java Program to Convert Decimal to Binary without using Integer.toBinaryString()
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class DecimalToBinaryManual {
    public static void main(String[] args) {
        // Step 1: Read the decimal number from the user
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter a decimal number: ");
            int decimal = scanner.nextInt();
            
            // Step 2: Convert the decimal number to binary manually
            String binary = "";
            int number = decimal;
            while (number > 0) {
                int remainder = number % 2;
                binary = remainder + binary;
                number = number / 2;
            }
            
            // Step 3: Display the result
            System.out.println("Binary of " + decimal + " is " + binary);
        }
    }
}

Explanation

Step 1: Read the Decimal Number

  • The Scanner class is used to read an integer input from the user. The nextInt() method captures the input number.

Step 2: Convert Decimal to Binary

  • Using Built-in Method:
    • The Integer.toBinaryString() method converts a decimal number directly to its binary equivalent.
  • Manual Conversion:
    • The program repeatedly divides the decimal number by 2, storing the remainder (either 0 or 1) and appending it to the binary string.
    • The process continues until the number becomes 0.
    • The remainders are appended to the binary string from left to right to form the correct binary number.

Step 3: Display the Result

  • The program prints the binary equivalent of the decimal number using System.out.println().

Output Example

Example 1: Using Built-in Method

Enter a decimal number: 10
Binary of 10 is 1010

Example 2: Using Manual Conversion

Enter a decimal number: 15
Binary of 15 is 1111

Example 3: Handling Zero

Enter a decimal number: 0
Binary of 0 is 0

Conclusion

These Java programs demonstrate how to convert a decimal number to its binary equivalent using both a built-in method and a manual conversion process. The solutions provided cover basic concepts such as loops, arithmetic operations, and string manipulation, making them valuable exercises for beginners learning Java programming.

Leave a Comment

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

Scroll to Top