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`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.0/org/apache/commons/lang3/StringUtils.html) is a helper method, which will print a character n times. So I create a string with n times a `X` and then I do it same with o for the rest and concatenate it for output. The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?