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); o =StringUtils.repeat("0", 9-i); System.out.println(x+o); } } StringUtils.repeat is a helper method, this will print a character n times. So i create a string with n times a `X` and the i do it same with o for the rest and concatinate it for Output.