04. BinaryNumbers
Print a list of binary numbers, in order. It uses a method to convert a decimal number to its binary equivalent.
Description: Print a list of binary numbers, in order. It uses a method to convert a decimal number to its binary equivalent.
Netbeans > file > new project > project > java application <next> > Project Name: BinaryNumbers > <finish>
Type in code:
package asu.binarynumbers;
public class BinaryNumbers {
public static void main(String[] args) {
int n = 2015;
String nBinary = convertToBinary(n);
int maxDigits = nBinary.length();
int p = 10; // numbers per line
for (int i = 1; i <= n; i++){
System.out.format("%" + (maxDigits + 1) + "s", convertToBinary(i));
if((i % p) == 0) {
System.out.println("");
}
}
}
public static String convertToBinary(int k){
String binary = "";
while (k > 0) {
if (k % 2 == 1){
binary = 1 + binary;
} else {
binary = 0 + binary;
}
k = k / 2;
}
return binary;
}
}Sample Ouput
Last updated
Was this helpful?