×

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

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Java exception list

Java uses exceptions, like the majority of contemporary programming languages, to deal with both errors and "extraordinary events." When an exception arises inside the program, it messes up the regular...

6 minutes read.