Kyle
Kyle

Reputation: 57

Print a Z shape pyramid using * stars

I am trying to write a program that outputs a Z pattern that is n number of * across the top, bottom, and connecting line using for loops.

Example:

Enter a number: 6

******
    *
   *
  *
 *
******

This is my current code, it's producing a half pyramid upside down.

import java.util.*;

public class ZShape {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);

      System.out.print("Enter a number: ");
      int n = input.nextInt(); 

      for (int x = 0; x <= n; x++) {
         for (int y = n; y >= 1; y--) {
            if (y > x) {
               System.out.print("* ");
            }
            else
               System.out.print(" ");
         } 
         System.out.println(); 
      }      
   }
}

Upvotes: 2

Views: 31910

Answers (5)

Mr.A
Mr.A

Reputation: 1

public class Main
{
    public static void main(String[] args) {
        int i, j,n=5;
        for(i = 1; i <= n; i++){
            for(j = 1; j <= n ; j++){
                if(i==1 || i+j == n+1 || i==n)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Upvotes: 0

Sanjoy Paul
Sanjoy Paul

Reputation: 1

Here is a logic to print Z shape:

  1. when i = 1, then the First line will print only "#"
  2. when i = n, then the Last line will print in same way by "#" only
  3. when i = j, that means it will execute one diagonal line by "#" only

Code:

public static void main(String[] args) {
    
    Scanner scr = new Scanner(System.in);
    
    int n = scr.nextInt();
    
    for (int i = 1; i <= n; i++) {
        for(int j = n; j >= 1; j--) {
            
            if (i == 1 || i == n || i == j) {
                System.out.print("# ");
            }
            
            else {
                System.out.print("  ");
            }
        }
        System.out.println();
    }
}

Upvotes: 0

public class Star {
public static void main(String[] args) {

      for (int i = 0; i <=4; i++) {
            for (int j = 0; j <=4; j++) 
            {
                if (i==4 ||   (i+j)==4 || i==0) 
                {
                    System.out.print(" * ");
                } 
                else 
                {
                    System.out.print("    ");
                }
            }
            System.out.println(" ");
        }
}
}

Upvotes: 0

Ryan
Ryan

Reputation: 2084

How about using three loops instead?

for (int x = 0; x < n; x++) {
    System.out.print("*");
}
System.out.println();
for (int x = n-3; x >= 0; x--) {
    for (int y = x; y >= 0; y--) {
        System.out.print(" ");
    }
    System.out.println("*");
}
for (int x = 0; x < n; x++) {
    System.out.print("*");
}

Upvotes: 1

Tunaki
Tunaki

Reputation: 137064

This is the logic in the following code:

  • Loop over each row of the output (so from 0 to n excluded so that we have n rows)
  • Loop over each column of the output (so from 0 to n excluded so that we have n columns)
  • We need to print a * only when it is the first row (x == 0) or the last row (x == n - 1) or the column is in the opposite diagonal (column == n - 1 - row)

Code:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a number: ");
    int n = input.nextInt();
    for (int row = 0; row < n; row++) {
        for (int column = 0; column < n; column++) {
            if (row == 0 || row == n - 1 || column == n - 1 - row) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

Sample output for n = 6:

******
    * 
   *  
  *   
 *    
******

(Note that this output has trailing white-spaces for each row, you did not specify whether they should be included, but it is easy to remove them by adding another check).

Upvotes: 7

Related Questions