2

I'm trying to write a code where I enter an number 'n' and it shows me 'n' binary numbers of the length 'n'. The sequence of the numbers doesnt matter.

e.g. I enter 3 (n=3)

000 001 010 011 100 101 110 111 This is what I got so far

    String b1="1",b2="0";
    String combination="0";
    String output="0"

    for (int k=1; k<=n; k++) {

            for (int z=1; z<=n; z++)
            {
                combination= b1+b2;
            }
        output = output+combination;





    }
    System.out.println(output);

But I get this output: 01010101010101010

JangoCG
  • 823
  • 10
  • 23
  • So you want to add newlines `\r\n`? http://stackoverflow.com/questions/247059/is-there-a-newline-constant-defined-in-java-like-environment-newline-in-c – Thomas Weller Nov 10 '16 at 16:19
  • 2
    Your approach looks all wrong. You're just combining "1" and "0" a bunch of times. There are 2^n values with n bits (in your case 2^3 == 8). – Michael Nov 10 '16 at 16:21
  • Even in your correct example output, you are not outputting n numbers of length n. You are outputting all (2^n) binary numbers of length n. – Michael Nov 10 '16 at 16:46
  • 1
    Can you please accept any applicable answer? – MaxZoom Dec 14 '16 at 18:03

3 Answers3

1

There are already tools to print binary representations of numbers in the Integer() class.

So for n=3 you want 3 sets of binary digits outputted, with 3 bits in each output. toBinaryString() takes an int parameter and returns "the string representation of the unsigned integer value represented by the argument in binary (base 2)". You will need to make some adjustments to get the proper padding in front of binary representations that are only 2 bits in length, i.e. 0, 1, 2, 3 which are 00, 01, 10, 11, respectively.

Edit: Once I copied my snippet to an actual Eclipse project I noticed my logic for padding was incorrect. I have fixed it. This code is almost exactly what you want. But I think you'll have to do some finessing to get the amount of output digits the way you want it.

int n = 4;
int numBits;
String paddingZero = "0";
String binary;

for(int i = 0; i <= n; i++)
{
    numBits = n / 2;
    if(numBits < 2)
    {
        numBits = 2; // Binary should never display less than 2 bits of digits for clarity.
    }

    binary = Integer.toBinaryString(i);
    if(binary.length() < numBits)
    {
        do  
        {
            binary = paddingZero + binary;
        }
        while(binary.length() < numBits);
    }

    System.out.print(binary + " "); // Appends the String representation of the binary digit to the paddingZeroes
}

Output

@ n = 3
00 01 10 11 

@ n = 4
00 01 10 11 100 

@ n = 7
000 001 010 011 100 101 110 111 

Once n > 8 the numBits logic will need to change some. This will get you started.

jseashell
  • 745
  • 9
  • 19
0

You are a little off track. The following link has a few different working examples of what I believe you are trying to achieve:

https://codereview.stackexchange.com/questions/24690/print-all-binary-strings-of-length-n

Community
  • 1
  • 1
LiXie
  • 136
  • 2
  • 14
0

As the maximum value of n-bit number is 2^n - 1 you can loop from zero to that value and display it in a binary format

public static void main (String[] args)
{
    int n = 4;
    String printFormat = "%"+n+"s";
    int max = (int) Math.pow(2, n);
    for (int i=0; i < max; i++) {
      String s = String.format(printFormat, Integer.toString(i, 2)).replace(' ', '0');  
      System.out.println(s);
    }
}

DEMO

MaxZoom
  • 7,619
  • 5
  • 28
  • 44