w3resource

Java: Convert a decimal number to binary numbers

Java Basic: Exercise-19 with Solution

Write a Java program to convert an integer number to a binary number.

Decimal number: The decimal numeral system is the standard system for denoting integer and non-integer numbers. It is also called base-ten positional numeral system.

Binary number: In digital electronics and mathematics, a binary number is a number expressed in the base-2 numeral system or binary numeral system. This system uses only two symbols: typically 1 (one) and 0 (zero).

Test Data:
Input a Decimal Number : 5

Pictorial Presentation: of decimal to binary number

Java: Convert a decimal number to binary numbers
Java: Decimal number to binary numbers

Sample Solution:

Java Code:

import java.util.Scanner;

public class Exercise19 {
    public static void main(String args[]) {
        // Declare variables to store decimal number, quotient, and an array for binary digits
        int dec_num, quot, i = 1, j;
        int bin_num[] = new int[100];
        
        // Create a Scanner object to read input from the user
        Scanner scan = new Scanner(System.in);

        // Prompt the user to input a decimal number
        System.out.print("Input a Decimal Number: ");
        dec_num = scan.nextInt();

        // Initialize the quotient with the decimal number
        quot = dec_num;

        // Convert the decimal number to binary and store binary digits
        while (quot != 0) {
            bin_num[i++] = quot % 2;
            quot = quot / 2;
        }

        // Display the binary representation of the decimal number
        System.out.print("Binary number is: ");
        for (j = i - 1; j > 0; j--) {
            System.out.print(bin_num[j]);
        }
        System.out.print("\n");
    }
}

Explanation:

In the exercise above -

  • First the code takes an integer or decimal number ('dec_num') as input from the user using the "Scanner" class.
  • It initializes an array 'bin_num' to store the binary digits of the converted number and other necessary variables.
  • Next it enters a loop to perform the decimal-to-binary conversion:
  • In each iteration, it calculates the remainder of 'dec_num' when divided by 2 (which gives the least significant binary digit) and stores it in the 'bin_num' array.
  • It then updates 'dec_num' by dividing it by 2 (which effectively shifts it to the right).
  • The loop continues until 'dec_num' becomes zero, effectively converting the entire decimal number to binary.
  • After the loop, it prints the binary representation of the decimal number by iterating through the 'bin_num' array in reverse order (from the most significant digit to the least significant digit).

Sample Output:

Input a Decimal Number : 5                                                                                    
Binary number is: 101  

Flowchart:

Flowchart: Java exercises: Convert a decimal number to binary numbers

Java Code Editor:

Previous: Write a Java program to multiply two binary numbers.
Next: Write a Java program to convert a decimal number to hexadecimal number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.