Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created October 31, 2012 01:19
Show Gist options
  • Save yao2030/3984239 to your computer and use it in GitHub Desktop.
Save yao2030/3984239 to your computer and use it in GitHub Desktop.
use recursion to print out the binary representation of an integer
public class BP
{
public static String binary(int N)
{
if(N / 2 == 0) return "" + N;
return binary(N/2) + "" + N % 2;
}
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
String a = binary(N);
StdOut.println(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment