Q:

How to create a program i n Java which gets all combinations of 1 and 2 of given number of digits

Let's say n= 7, so I need to display all possible combinations of 7 digits made out of 1s and 2s: 
1111111
1111112
1111121
1111211
1112111
.......
2222222
Here is my code, but it is hardcoded nested 7 times loops. So my count=128 which is 2^7 But how to make it programmatically for n number of times nesting? I think it needs some recursion, but how to do it?
int count = 0;
    for (int j = 1; j<=2; j++) {
        for (int z= 1; z<=2; z++) {
            for (int i = 1; i<=2; i++) {
                for (int k = 1; k<=2; k++) {
                    for (int l= 1; l<=2; l++) {
                        for (int m = 1; m<=2; m++) {
                            for (int y = 1; y<=2; y++) {
                                count++;

                                System.out.println(count+":"+j+z+i+k+l+m+y+",");

                            }
                        }
                    }       
                }
            }
        }
    }

A:

Usually, "all combinations of N symbols" is the same than "counting in base N".
Since you have two symbols, even easier. Count from 0 to X, get the binary representation of each number, replace 0 by 2. Once the binary representation gets longer than the number of values you desire, stop.
Do not forget to add leading 2 to numbers which are too short.

