Various Star Pattern Programming in Java

In this Java tutorial, I am going to discuss some of the star pattern programmings. Star pattern programming is generally discussed in starting when we start learning any programming language and is also asked in our semester exams.

Star Pattern in Java – 1

 

*
**
***
****
*****

Algorithm

  1. Scan the value of the number of rows, we have taken 5 here.
  2. Now, in each row number of stars to be printed are equal to row index.
  3. Print the stars according to row index.

Java Code to print the Star pattern-1

import java.util.Scanner;

  public class Codespeedy
{
      public static void print_pattern ( int n )
   {
       for ( int i=0 ;i<n;i++)
     {
         for(int j=0 ;j<=i;j++)
       {
         System.out.print("*");  
       }
       
         System.out.println();
     }
   }

     public static void main ( String [] args )
   {
       Scanner scan = new Scanner (System.in);
       
         int n = scan.nextInt();
         
         print_pattern(n);
   }
   
}

Pattern-2

      *
     **
    ***
   ****
  *****

Algorithm

  1. Scan the value of the number of rows ‘n’.
  2. Now, for each row number of whitespaces ” ” are (n-row_index) and stars are ( row_index).
  3. First print the whitespaces then the stars according to values.

Java Code to print the Star pattern-2

import java.util.Scanner;

  public class Codespeedy
{
      public static void print_pattern ( int n )
   {
       for ( int i=0 ;i<n;i++)
     {
         for(int j=0 ;j<=n-i;j++)
       {
         System.out.print(" ");  
       }
       
        for(int j=0 ;j<=i;j++)
       {
         System.out.print("*");  
       }
      
           System.out.println();
     }
   }

     public static void main ( String [] args )
   {
       Scanner scan = new Scanner (System.in);
       
         int n = scan.nextInt();
         
         print_pattern(n);
   }
   
}

Pattern-3

*****
****
***
**
*

Algorithm

  1. Scan the value of the number of rows ‘n’.
  2. for each row number of stars are equal to n-row_index value.
  3. Print the stars according to values for each row.

Java Code to print the Star pattern-3

import java.util.Scanner;

  public class Codespeedy
{
      public static void print_pattern ( int n )
   {
       for ( int i=0 ;i<n;i++)
     {
         for(int j=0 ;j<n-i;j++)
       {
         System.out.print("*");  
       }
         System.out.println();
     }
   }

     public static void main ( String [] args )
   {
       Scanner scan = new Scanner (System.in);
       
         int n = scan.nextInt();
         
         print_pattern(n);
   }
   
}

Pattern-4

*****
 ****
  ***
   **
    *

Algorithm

  1. Scan the value of the number of rows ‘n’.
  2. For each row number of whitespaces are equal to (row_index) and the number of stars is (n-row_index).
  3. First print the whitespaces then stars according to their values for each row.

Java Code to print the Star pattern-4

import java.util.Scanner;

  public class Codespeedy
{
      public static void print_pattern ( int n )
   {
       for ( int i=0 ;i<n;i++)
     {
         for(int j=0 ;j<i;j++)
       {
         System.out.print(" ");  
       }
       
         for(int j=0 ;j<n-i;j++)
       {
         System.out.print("*");  
       }
       
         System.out.println();
     }
   }

     public static void main ( String [] args )
   {
       Scanner scan = new Scanner (System.in);
       
         int n = scan.nextInt();
         
         print_pattern(n);
   }
   
}

Run this code to see the output pattern.

You can also learn,

Leave a Reply

Your email address will not be published. Required fields are marked *