Top Patterns in Java Code: How to Print Star(*)/Number/Character

Star Patterns in Java


First, let us begin with some of the basic and the commonly asked pattern program in Java.

Such as:

  • Pyramid Star Program,
  • Left Triangle Star Program,
  • Right Triangle Star Program, etc.

Below Example prints the star(*), you can replace star to number or character as your wish to print.

1. Pyramid Program:

pyramid pattern in Java


Code:

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


2. Left Triangle Star Pattern:

                        
left triangle pattern in Java

Code:

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

3. Right Triangle Star Pattern:

right triangle pattern in Java

Code:

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

Thank You For Reading !!! Keep Reading !!!

Comments

Popular posts from this blog

An overview of Three Phase Commit (3PC) Protocol

How beautiful is Nepal? 12 Most Beautiful Places On Nepal