×

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic. In these loops, the first loop work for rows and the second loop work for columns.

Let’s see some star patterns in java.

1. Right Triangle Pattern without space between stars

public class RightTriangle{
    public static void main(String args[]){
        // i works for rows
        // j works for columns 
        int i, j, m = 6; // m = number of rows to be printed
        for(i = 1;i <= m;i++){
            for(j = 1;j <= i;j++){
       // for not getting space between stars, we do not use any space after a   star
                System.out.print("*");
            }
            System.out.println();
        }
    }
}              

Output:

Star Program in Java

2. Right Triangle pattern with space between stars

public class RightTriangle{
    public static void main(String args[]){
         // i works for rows
        // j works for columns 
        int i, j, m = 6; // m = number of rows to be printed
        for( i = 1;i <= m;i++){
            for(j = 1;j <= i;j++){
                // for getting space between stars, we used a space after a star
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

Star Program in Java

3. Square Pattern Using stars

public class SquarePattern{
    public static void main(String args[]){
        for(int i = 1;i <= 5;i++){
            for(int j = 1;j <= 5;j++){
                System.out.print("* "); // Give space
            }
            System.out.println();
        }
    }
}

Output:

Star Program in Java

4. Decreasing Triangle Patterns Using Stars

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

Output:

Star Program in Java

5. Hill Pattern Using stars

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

Output:

Star Program in Java

6. Reverse Hill Pattern Using stars

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

Output: 

Star Program in Java

7. Reverse Hill Pattern Using stars

public class ReverseHillPattern {
  public static void main(String[] args) {
    int size = 5;
    for (int i = 1; i <= size; i++) {
      for (int j = size; j > i; j--) {
        System.out.print("  ");
      }
      for (int k = 0; k < i * 2 - 1; k++) {
        System.out.print("* ");
      }
      System.out.println();
    }
    for (int i = 1; i <= size - 1; i++) {
      for (int j = 0; j < i; j++) {
        System.out.print("  ");
      }
      for (int k = (size - i) * 2 - 1; k > 0; k--) {
        System.out.print("* ");
      }
      System.out.println();
    }
  }
}

Output:

Star Program in Java

8. Right Pascal Triangle Pattern Using Stars

public class RightPascalTriangle  
{  
public static void main(String[] args)  
{  
int i, j, rows = 5;  
for (i= 0; i<= rows-1; i++)  
{  
for (j=0; j<=i; j++)   
{  
System.out.print("*"+ " ");  
}   
System.out.println("");   
}   
for (i=rows-1; i>=0; i--)  
{  
for(j=0; j <= i-1;j++)  
{  
System.out.print("*"+ " ");  
}  
System.out.println(" ");  
}  
}  
}  

Output:

Star Program in Java

9. Right Pascal Triangle Pattern Using Stars

public class LeftPascalTriangle 
{  
public static void main(String[] args)  
{  
int i, j, k, rows = 5;
for (i= 1; i<= rows ; i++)  
{  
for (j=i; j <rows ;j++)              
{  
System.out.print(" ");  
}  
for (k=1; k<=i;k++)   
{  
System.out.print("*");   
}   
System.out.println("");   
}   
for (i=rows; i>=1; i--)  
{  
for(j=i; j<=rows;j++)  
{  
System.out.print(" ");  
}  
for(k=1; k<i ;k++)   
{  
System.out.print("*");  
}  
System.out.println("");  
}  
}  
}

Output:

Star Program in Java

10. Hallow Triangle Pattern

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

Output:

Star Program in Java

Related Topics

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Delete a Cycle from a Linked List in Java

Assume a method detectAndRemoveLoop(), which examines if a given Linked List includes a loop and, if so, eliminates this Loop and returns true. This returns false if the list does...

7 minutes read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Diamond problem in Java

The Diamond Problem in Java is connected to multiple inheritances. It is also referred to as the "deadly diamond dilemma" or even the "deadly diamond of death”. The solution for...

5 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Java Math sinh() Method

The sinh() method of Java Math class returns the hyperbolic sine of the specified double value. Syntax: public static double sinh(double x) Parameters: The parameter ‘a’ represents the number whose hyperbolic sine is to...

2 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.