Q:

Need help in pattern printing

I need to print the following pattern and i almost did with the coding part.
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1

Following is the program I tried
public class MyPattern {
    public static void main(String[] args) {
        for (int i = 0; i <= 7; i++) {
            for (int j = 1; j <= 7 - i; j++) {
                System.out.print(" ");
            }

            for (int j = 0; j <= i; j++) {
                int n = (int) Math.pow(2.0D, j);

                if (n > 100) {
                    System.out.print(" " + n);
                } else if (n > 10) {
                    System.out.print(" " + n);
                } else {
                    System.out.print(" " + n);
                }

            }

            for (int j = i - 1; j >= 0; j--) {
                int n = (int) Math.pow(2.0D, j);
                if (n > 100) {
                    System.out.print(" " + n);
                } else if (n > 10) {
                    System.out.print(" " + n);
                } else {
                    System.out.print(" " + n);
                }

            }
            System.out.print('\n');
        }
    }
}

When running the program I got the following output
        1
       1 2 1
      1 2 4 2 1
     1 2 4 8 4 2 1
    1 2 4 8 16 8 4 2 1
   1 2 4 8 16 32 16 8 4 2 1
  1 2 4 8 16 32 64 32 16 8 4 2 1
 1 2 4 8 16 32 64 128 64 32 16 8 4 2 1

But I need the output aligned to left (as specified first). Please help.

A:

Well it's clearly caused by this part of code:
for (int j = 1; j <= 7 - i; j++) {
    System.out.print(" ");
}

Have you tried running it without it?

