Java for students logo
Need help with Java programming assignment? Get affordable Java homework help.

Decimal to binary converter

Abstract

Java program, asking user for a positive integer and converting it to binary representation.

Source code

DecimalToBinaryConverter.java

import java.util.Scanner;

 

public class DecimalToBinaryConverter {

      public static void main(String[] args) {

            Scanner in = new Scanner(System.in);

            int decimalNumber;

            String binaryNumber;

            System.out.print("Enter a positive integer: ");

            decimalNumber = in.nextInt();

            if (decimalNumber <= 0)

                  System.out.println("ERROR: entered integer is nonpositive.");

            else {

                  binaryNumber = "";

                  // algorithm step by step

                  // initial: binaryNumber = "", decimalNumber = 123

                  // step 1 : binaryNumber = "1 ", decimalNumber = 61

                  // step 2 : binaryNumber = "11 ", decimalNumber = 30

                  // step 3 : binaryNumber = "011 ", decimalNumber = 15

                  // step 4 : binaryNumber = "1011 ", decimalNumber = 7

                  // step 5 : binaryNumber = "1 1011 ", decimalNumber = 3

                  // step 6 : binaryNumber = "11 1011 ", decimalNumber = 1

                  // step 6 : binaryNumber = "111 1011 ", decimalNumber = 0

                  // stop   : (decimalNumber != 0)

                  while (decimalNumber != 0) {

                        // add spaces to separate 4-digit groups

                        if (binaryNumber.length() % 5 == 0)

                             binaryNumber = " " + binaryNumber;

                        // extract last digit in binary representation

                        // and add it to binaryNumber

                        binaryNumber = (decimalNumber % 2) + binaryNumber;

                        // cut last digit in binary representation

                        decimalNumber /= 2;

                  }

                  System.out.println("Binary: " + binaryNumber);

            }

      }

}

Download DecimalToBinaryConverter.java

Sample run

Enter a positive integer: 123

Binary: 111 1011