If you can use commons-lang3: public static void main(String[] args) { // Algorithm 1 String o = null; for (int i = 0; i < 10; i++) { String x = StringUtils.repeat("X", i+1); for (int j = 0; j < 9; j++) { o =StringUtils.repeat("0", j-i+1); } System.out.println(x+o); } }