• + 0 comments

    // Use Java version 15. + First, I realized that the "#" character should be printed from 1 up to n each line. + Therefore, the number of spaces printed on each line equals n minus the number of "#" characters. + To reduce the number of System.out.println calls (which improves I/O effiency) we can use the StringBuilder with the repeat() method (available since Java 11).

    // Source Code: public static void staircase(int n) { // Write your code here StringBuilder sb = new StringBuilder(); for (int i = 0 ; i< n ; i++) {

        sb.append(" ".repeat(n-i-1))
        .append("#".repeat(i+1)).append("\n") ;
    }
    
    System.out.println(sb);
    
    
    
    
    }