×

Star Pattern Programs in Java

Star Pattern Programs in Java

The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier.

Right Triangle Star Pattern

Filename: StarPatternExample.java

public class StarPatternExample
{             
public static void main(String argvs[])
{
               int row = 7; // numer of rows to be printed
               int r, c; 
               for(r = 0; r < row; r++) //loop for rows
               {
                               for(c = 0; c <= r; c++) //  loop for columns
                               {      
                                              System.out.print("* "); // prints stars
                               }   
                               System.out.println(); // at the end of each row throws the cursor to the next line
               }
}
}

Output:

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

Mirrored Right Triangle Star Pattern

Filename: StarPatternExample2.java

public class StarPatternExample2
{             
public static void main(String argvs[])
{
                int r, c;
               int row = 7; //number of rows to be printed
                for (r = 0; r < row; r++) //loop for rows
               {
                               for (c = r; c < row; c++) //loop for columns
                               {
                                    System.out.print("* "); // Printing stars
                               }
                               System.out.println(); // at the end of each row throws the cursor to the next line
               }
}
}

Output:

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

Mirrored Left Triangle Star Pattern

Filename: StarPatternExample3.java

public class StarPatternExample3
{             
public static void main(String argvs[])
{
               int row = 7; //number of rows to be printed
               int r, c; 
               for(r = 0; r < row; r++) //loop for rows
               {
                               for(int ind = 0; ind < 2 * r + 1; ind++) //  This loop handles indentation
                               {      
                                              System.out.print(" "); // prints spaces
                               }
                               for(c = r; c < row; c++) //  loop for columns
                               {      
                                              System.out.print("* "); // Displaying stars
                               }    
                               System.out.println(); // at the end of each row throws the cursor to the next line
               }
}
}

Output:

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

Triangle Star Pattern

Filename: StarPatternExample4.java

public class StarPatternExample4
{             
public static void main(String argvs[])
{
               int rows = 7; // number of rows to be printed
                for (int r = 1; r <= rows; r++) // loop for rows
                {
                               for (int ind = r; ind < rows; ind++) // Handling indentation for each row
                               {
                                              System.out.print(" "); // Printing spaces
                               }  
                               for (int c = 1; c <= (2*r -1); c++) //loop for columns
                               {
                                              // Condition for displaying stars
                                              if( c == 1 || r == rows || c == (2*r - 1))
                                              {
                                                              System.out.print("*");
                                              }
                                              else // Handling inter-star gaps
                                              {
                                                              System.out.print(" ");
                                              }
                               }
                               System.out.println(); // at the end of each row throws the cursor to the next line
            }
}
}

Output:

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

Down Triangle Star Pattern

Filename: StarPatternExample5.java

public class StarPatternExample5
{             
public static void main(String[] args)
{
               int rows = 7; // number of rows to be printed     
               for (int r = rows; r >= 1; r--) // loop for rows
               {
                               for (int ind = r; ind < rows ; ind++) // Handling indentation for each row
                               {
                                              System.out.print(" ");
                               }  
                               for (int c = 1; c <= (2*r -1); c++) //loop for columns
                               {
                                              // Conditions for printing stars
                                              if( c ==1 || r == rows || c ==(2*r - 1))
                                              {
                                                             System.out.print("*");
                                              }
                                              else // Dealing with inter-star gaps
                                              {
                                                             System.out.print(" ");
                                              }
                               }
                               System.out.println(); // at the end of each row throws the cursor to the next line
               }
}
}

Output:

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

Diamond Star Pattern

Filename: StarPatternExample6.java

public class StarPatternExample6
{             
public static void main(String argvs[])
{
               int rows = 8; // number of rows to be printed     
               //logic to print the first half of the pattern
               for (int r = 1; r <= rows; r++) // Handling rows
               {
                               for (int ind = rows; ind > r; ind--) // Handling indentation for each row
                               {
                                              System.out.print(" ");
                               }
                               System.out.print("*"); // Printing stars
                               for (int sp = 1; sp < 2*(r -1); sp++) // Handling inter-star spaces
                               {
                                              System.out.print(" "); // Printing spaces        
                               }
                               if(r == 1) // Only one star for the first row
                               {
                                              System.out.println("");            
                               }
                               else // Every row contains 2 stars barring the first and last row.
                               {
                                              System.out.println("*");
                               }
               }
               // logic to print the second half of the pattern
               for (int r = rows-1; r >= 1; r--) // loop for rows
               {
                               for (int ind = rows; ind > r ; ind--) // Handling indentation
                               {
                                              System.out.print(" ");
                               }
                               System.out.print("*"); // Printing star
                               for (int sp = 1; sp < 2*(r - 1); sp++) // Handling inter-star spaces
                               {
                                              System.out.print(" ");
                               }
                               if(r == 1) // Only one star for the last row
                                              System.out.println("");
                               else // Every row contains 2 stars barring the first and last row.
                                              System.out.println("*");
               }
}
}

Output:

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

Star Pattern – K Shaped

Filename: StarPatternExample7.java

public class StarPatternExample7
{
public static void main(String argvs[])
{
               int row = 9; //number of rows to be printed
               // logic to print the first half of the k-shaped pattern
               for(int r = 0; r < row; r++)
               {
                               // loop for columns
                               for(int c = 0; c < row - r; c++)
                               {
                                              System.out.print("* "); // Printing stars
                               }                            
                               System.out.println(); // moves the cursor to the next line
               }
               // logic to print the second half of the k-shaped pattern
               for(int r = 1; r < row; r++)
               {                            
                               for(int c = 0; c <= r; c++)
                               {
                                              System.out.print("* "); // Printing stars
                               }                            
                               System.out.println(); // moves the cursor to the next line
               }
}
}             

Output:

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

Star Pattern – Hour-Glass Shaped

Filename: StarPatternExample8.java

public class StarPatternExample8
{
public static void main(String argvs[])
{
               int rows = 9; // number of rows to be printed
               //logic to print the first half of the row
               for (int r= 0; r <= rows-1; r++) // loop for rows
                {
                               for (int ind = 0; ind < r; ind++) // Dealing with indentation
                               {
                                              System.out.print(" ");
                               }
                               for (int st=r; st<=rows-1; st++) // Loop for printing stars and spaces
                               {
                                              System.out.print("*" + " ");
                               }
                               // This statement induces line break at the end of each row
                               System.out.println("");
               }
               //logic to print second half of the pattern   
               for (int r= rows-1; r >= 0; r--)
               {
                               for (int ind = 0; ind < r; ind++) // Dealing with indentation
                               {
                                              System.out.print(" ");
                               }
                               for (int st = r; st <= rows-1; st++) // Loop for printing stars
                               {
                                              System.out.print("*" + " ");
                               }
                               // This statement induces line break at the end of each row
                               System.out.println("");
                }
}
}             

Output:

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

Star Pattern – Alphabet A Shaped

Filename: StarPatternExample9.java

public class StarPatternExample9
{
public static void main(String argvs[])
{
               int rows = 9; // number of rows to be printed
               for (int r = 0; r <= rows; r++) // loop for rows
               {
                               for (int j = 0; j <= rows / 2; j++)
                               {
                                              // prints the two vertical lines of the alphabet A
                                              if ((j == 0 || j == rows / 2) && r != 0 ||
                                              // Displays the top most line of the alphabet A
                                              r == 0  && j != rows / 2 ||
                                              // printing the middle line
                                              r == rows / 2)
                                                             System.out.print("*"); // Printing stars
                                              else
                                                             System.out.print(" "); // Printing inter-star spaces
                               }
                               System.out.println(); // moves cursor to the next line
               }
}
}             

Output:

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

Related Topics

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Java Integer toOctalString() method

The toOctalString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 8. Syntax public static String toOctalString (int  i) Parameters The parameter ‘i’ represents...

1 minute read.

Add numbers represented by Linked Lists in Java

For calculating the sum of the two numbers that are represented by a linked list, and then store the result in a new linked list. A linked list's head node...

7 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java Program to find the smallest element in a tree

The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the...

3 minutes read.

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.