Integer i; String str = ""; for (int x =0, x < 8, x++) { i = (Integer) Math.pow(2,x); // casting result which returns a double to an Integer. if (x ==0) { str = i.toString(); System.out.println(str); } else if (x ==1) { str = (Integer) Math.pow(2,x-1).toString() + " " + i.toString(); System.out.println(str); } else if (x==2) { str = (Integer) Math.pow(2,x-2).toString() + " " + (Integer) Math.pow(2,x-1).toString() + " " + i.toString(); System.out.println(str); } else if (x==3) { str = (Integer) Math.pow(2,x-3).toString() + " " + (Integer) Math.pow(2,x-2).toString() + " " + (Integer)Math.pow(2, x-1).toString() + " " + i.toString(); System.out.println(str); } else if (x==4) { str = (Integer) Math.pow(2,x-4).toString() + " " + (Integer) Math.pow(2,x-3).toString() + " " + (Integer) Math.pow(2,x-2).toString() + " " + (Integer)Math.pow(2, x-1).toString() + " " + i.toString(); System.out.println(str); } else if (x==5) { str = (Integer) Math.pow(2, x-5).toString() + " " +(Integer) Math.pow(2,x-4).toString() + " " + (Integer) Math.pow(2,x-3).toString() + " " + (Integer) Math.pow(2,x-2).toString() + " " + (Integer)Math.pow(2, x-1).toString() + " " + i.toString(); System.out.println(str); } else if (x==6) { str = (Integer) Math.pow(2,x-6).toString() + " " +(Integer) Math.pow(2, x-5).toString() + " " +(Integer) Math.pow(2,x-4).toString() + " " + (Integer) Math.pow(2,x-3).toString() + " " + (Integer) Math.pow(2,x-2).toString() + " " + (Integer)Math.pow(2, x-1).toString() + " " + i.toString(); System.out.println(str); } else { str = (Integer) Math.pow(2,x-7).toString() + " " + (Integer) Math.pow(2,x-6).toString() + " " +(Integer) Math.pow(2, x-5).toString() + " " +(Integer) Math.pow(2,x-4).toString() + " " + (Integer) Math.pow(2,x-3).toString() + " " + (Integer) Math.pow(2,x-2).toString() + " " + (Integer)Math.pow(2, x-1).toString() + " " + i.toString(); System.out.println(str); } }