Java Programs | star pattern design code | Interview coding Q&A | beginners

Yogi Siddeswara 0

Java Code :

 
  package starpatterns;

public class StarPattern1 {

  public static void main(String[] args) {
    int k = 2; // Initialize k to 2

    // Loop for rows (10 to 1)
    for (int i = 10; i >= 1; i--) {
      if (i > 5) {
        // Print decreasing asterisks for top half
        for (int j = 1; j <= i - 5; j++) {
          System.out.print("* ");
        }
      } else {
        // Print increasing asterisks for bottom half
        for (int j = 1; j < k; j++) {
          System.out.print("* ");
        }
        k++;
      }
      // Move to the next row
      System.out.println();
    }
  }
}

(code-box)

Expected Output :

  
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 
(code-box)

Post a Comment

0 Comments