Printing all binary strings of length n
Speaking from a straight Java perspective, one thing you can do is change your loop header. You evaluate the power every iteration and could do something like:

for (int i = 0, end = Math.pow(2, n); i &lt; end; i++)


You also use standard String concatenation which is slow because once compiled, when you concatenate two strings a StringBuffer is created, and both strings are added to it, and then resulting string is returned. You save a step by using a StringBuffer from the get go which would you replace B = ""; with B = new StringBuffer(); (of course changing it's type accordingly). Instead of adding strings to you call insert (which can be chained) like B.insert(0, "1")

You're appending a char ('0' or '1') which you evaluate with modular division, you can just prepend it like B.insert(0, (temp % 2)) and save that entire if.

I probably said more than I should have for homework, but you appear to have already solved your problem and so I was just giving advice in addition to that.

EDIT

My solution would be as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number &gt;&gt; ");
        int value = in.nextInt();
        in.close();
        Binary binary = new Binary(value);
        System.out.println(binary.getResult());
    }
}

class Binary {
    private int value = 0;
    private StringBuilder binaryValues;

    public Binary(int value) {
        this.value = value;
        this.binaryValues = new StringBuilder();
        this.process();
    }

    private void process() {
        for (int i = 0, end = (1 &lt;&lt; this.value); i &lt; end; i++) {
            StringBuilder binary = new StringBuilder(Integer.toString(i, 2));
            while (binary.length() &lt; this.value)
                binary.insert(0, 0);
            this.binaryValues.append(binary).append("\n");
        }
    }

    public String getResult() {
        return this.binaryValues.toString();
    }
}


Maybe not shorter but no need to rewrite binary number generation when it's built in.