/************************************************************************* * Matthew J. Rutherford [2/22/09] - Modified from the file Binary.java: * * http://www.cs.princeton.edu/introcs/13flow/Binary.java.html * * Compilation: javac Binaries.java * Execution: java Binaries n [n2 ]* * * Prints out n in binary. * * % java Binary 5 106 * 101 * 1101010 * * % java Binary 0 * 0 * * % java Binary 16 * 10000 * * Limitations * ----------- * Does not handle negative integers. * * Remarks * ------- * could use Integer.toBinaryString(N) instead. * *************************************************************************/ public class Binaries { public static void main(String[] args) { for(int i=0; i 0) { // v is not present in n if (n < v) { System.out.print(0); } // v is present in n, so remove v from n else { System.out.print(1); n = n - v; } // next smallest power of 2 v = v / 2; } System.out.println(); } } }