How to convert a decimal number to its binary representation using java

import java.util.Scanner;

public class DecimalToBinaryConverter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        int decimal = scanner.nextInt();
        String binary = convertToBinary(decimal);
        System.out.println("Binary representation: " + binary);
        scanner.close();
    }

    private static String convertToBinary(int decimal) {
        return Integer.toBinaryString(decimal);
    }
}

 

Post your Answer