# 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:

## 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:

## 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:

## 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:

## 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:

## 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:

## 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:

## 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:

## 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:

## 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: