Sort by

recency

|

5199 Discussions

|

  • + 0 comments

    Rust

    fn staircase(n: i32) {
            for number in 1..=n {
                let line = "#".repeat(number as usize);  
                println!("{:>width$}", line, width = n as usize );
            };
    }
    
  • + 0 comments

    public static void staircase(int n) { // Write your code here for(int i=0;i(n-2)) System.out.print("#"); else System.out.print(" ");

        }
        System.out.println();
    }
    
    }******
    
  • + 0 comments

    Starting from 0 will lead to a blank line, so start with 1. That's whart I was doing... lol

    for i in range(1, n+1):
            print(' '*(n-i)+'#'*i)
    				
    	
    
  • + 0 comments

    ** public static void staircase(int n) {

        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<n;j++)
            {
               System.out.print(" ");
            }
    
            for(int j=i;j>=1;j--)
            {
               System.out.print("#");
            }
    
            System.out.println();
    
        }
    }**
    
  • + 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);
    
    
    
    
    }