×

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

Differences and Similarities between HashSet, LinkedHashSet and TreeSet in Java

HashSet Class: The HashSet is a class that executes the Setpoint of interaction. It is utilised to store the items in a hashtable; a hashtable is an information structure which holds...

10 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

How to set timer in Java

In this article, you will be very well equipped with the knowledge to set timer in java. The timer in java can be set by using timer class provided by...

3 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

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.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

What is anagram in Java?

In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java...

4 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

Java Viva Questions and Answers

Question: Explain Java programming language. Answer: It is a high-level programming language. This programming language is based on the principles of object-oriented programming. Large-scale applications can be developed by using this...

16 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.