int num_of_lines = 20; int start_num = 1; int spaces = 0;
for (int i = 0 ; i < num_of_lines; i ++ ) {
//Assumption:
//Each number occupies max 3 characters
//1 for number separator i.e space
//2 for actual number
spaces = num_of_lines - i - 1;
System.out.print(getSpace((3*spaces/2)));
for (int j = start_num ; j <= (start_num + i) ; j ++) {
//3 - chars required including actual number
//for single digit number prefix two spaces
//for double digit number prefix single space
System.out.print((j < 10 ? " " : " ") + j);
}
System.out.println();
}
}
private static String getSpace(int spaces) {
StringBuilder sb = new StringBuilder(); for (int i = 0 ; i < spaces ; i ++) {
sb.append(" ");
} return sb.toString();
}
}