Java Program to Print Alphabet Z Star Pattern

Program to Print Alphabet Z Star Pattern

In this article we are going to see how to print Z  star program or z pattern in java or how to print z pattern in java.

Examples to know:

  • Print Z Star Pattern In Java
  • Number Z = 5 & 4 | 6; Print(Z);
  • Number Z=2^3 Print(Z)
  • Number Z = 5 & 4 6 Print(Z)
  • Number Z=5&4 6 Print(Z)
  • Z Pattern Code
  • Print A To Z In Java etc.
Example-1

When row value=6      

******
    *
   * 
  *  
 *   
******
Example-2:

When row value=5

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

Now, let’s see the actual program printing Z Star pattern in java.

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first for loop to print the row value  .
  • Take a counter value  d = row -1 .
  • Take first inner for loop to print column value i.e., star  according to condition
    if (r == 0 || r == row  - 1|| c == d) else loop will print space .
  • For every iteration of outer for loop decrease the counter d.
  • Then go on printing the star symbol according to loop.

JAVA Code:

Method-1: Static Star Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,d ;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    d=row-1;
    //outer for loop 
   for (r = 0; r < row; r++) 
    {
        for (c = 0; c < row ; c++)
        {
            if (r == 0 || r == row  - 1 || c == d)
                System.out.print("*");
            else
                System.out.print(" ");
        }
        d--;
        System.out.print("\n");
    }
    }
}

Output :

Enter row :  5 

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

Method-2: User Input Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int r,c,d ;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    int row = s.nextInt();
     // entering the number of row
    System.out.print("Enter character : ");
    char z = s.next().charAt(0);
    d=row-1;
    //outer for loop 
   for (r = 0; r < row; r++) 
    {
        for (c = 0; c < row ; c++)
        {
            if (r == 0 || r == row  - 1 || c == d)
                System.out.print(z);
            else
                System.out.print(" ");
        }
        d--;
        System.out.print("\n");
    }
    }
}

Output :

Enter row :  5 
Enter character : *

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

C Code:

#include <stdio.h>
int main() {
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
   d=row-1 ;
  for (r = 0; r < row; r++) 
    {
        for (c = 0; c < row ; c++)
        {
            if (r == 0 || r == row  - 1 || c == d)
                 printf("*");
            else
                 printf(" ");
        }
        d--;
         printf("\n");
    }
   return 0;
}
Output :

Enter row :  5 

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

C++ Code:

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ,d ;
   cout << "Enter  rows: ";
   cin >> row;
   d= row -1 ;
   for (r = 0; r < row; r++) 
    {
        for (c = 0; c < row ; c++)
        {
            if (r == 0 || r == row  - 1 || c == d)
                cout << "*";
            else
                cout << " ";
        }
        d--;
        cout << "\n";
    }
   return 0;
}

Output :

Enter row :  5 

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

Recommended Reading On:

Try these:

  1. Z Pattern Program In Java
  2. How To Print Z Pattern In C
  3. Print The Alphabets A To Z In Star Pattern
  4. C Program To Print Z Pattern Using Numbers
  5. Print The Alphabet A To Z In Star Pattern In C
  6. Print Z Star Pattern In C++
  7. Alphabet Star Pattern In Java

Related Java Star Pattern Programs: