/* Online Java - IDE, Code Editor, Compiler Online Java is a quick and easy tool that helps you to build, compile, test your programs online. */ import java.util.Scanner; public class Main { public static void main(String[] args) { int j=0; int binary[] = new int[8]; //This is to define 8 bit array to hold binary value int i; Scanner reader = new Scanner(System.in); // Reading from System.in System.out.println("Enter a number: "); int num = reader.nextInt(); // Scans the next token of the input as an int. //once finished reader.close(); int length = binary.length; System.out.println("length: " + length); System.out.println("*** This program will convert decimal into binary"); //Execute a for loop to check modulus (i.e no remainder) //execute a loop from 0 to 8. This is the powers used in base 2 system to allow representation from 1 to 128. for (i=length-1; i>=0; i--) { //j++; // this will ensure that number inputted can be converted to binary System.out.println("Enter here"); int divisor = (int)(Math.pow(2,i)); System.out.println("divisor is:" + divisor); if (num - divisor >=0) { System.out.println("reach here"); System.out.println("value of i:" + i); //k++; binary[(length-1)-i] = 1; num=num-divisor; } } for (int k:binary) { System.out.println(k); //System.out.println(k); } } }