fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. int max = 6;
  12. int padLength = (int) Math.ceil(Math.log10(Math.pow(2, max) + 1)) + 2;
  13. for (int i = 0; i < max; i++) {
  14. for (int j = 1; j < max - i; j++) {
  15. System.out.print(pad(" ", padLength));
  16. }
  17. for (int k = 0; k <= i; k++) {
  18. System.out.print(pad(Math.pow(2, k), padLength));
  19. }
  20. for (int k = i - 1; k >= 0; k--) {
  21. System.out.print(pad(Math.pow(2, k), padLength));
  22. }
  23. System.out.println();
  24. }
  25. }
  26.  
  27. public static String pad(double d, int l) {
  28. Integer i = (int) d;
  29. return pad(i.toString(), l);
  30. }
  31.  
  32. public static String pad(String s, int l) {
  33. return String.format("%-" + l + "s", s);
  34. }
  35. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
                    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