×

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

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Balanced Parentheses in Java

One of the frequent programming issues, commonly referred to as the Balanced brackets issue, is the problem with the balanced parenthesis. Interviewers frequently provide this task, in which we must...

5 minutes read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

Java Base64 Encoding and Decoding

Introduction to Encoding and Decoding Encoding is the process of putting the sequence of characters like letters, numbers, punctuations, and other symbols into a specialised format for the efficient transmission or...

11 minutes read.

Java String hashCode() method

Java String hashCode() method returns hash code for current String. hash code for string object is computed as s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1] Using int...

2 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

Hidden classes in Java

There specifically are some APIs available in the market that generally is harmful to be used in our programs specifically literally, and until JDK 15, there, for all intents and...

4 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

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.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Java Static Keyword

It can be either said that static declares the value to be the same, not only in the instance of a class but also as the whole. To declare a variable...

6 minutes read.