Tag: java star pattern

  • Java Program to Print Right Triangle Star Pattern

    Java Program to Print Right Triangle Star Pattern

    In this tutorial, we will write a program to print the right triangle star pattern in java. Before that, you may go through the following topic in java.

    It also can be called mirrored right triangle number pattern in java or a half pyramid pattern in java. The program takes a user input for the number of rows and displays it accordingly.


    Java Program to Print Right Triangle Star Pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int i, j, num;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        num = sc.nextInt();
    
        for (i = 1; i <= num; i++)
        {
          for (j = 1; j <= i; j++)
          {
            System.out.print("*");
          }
    
          System.out.println("");
        }
      }
    }

    Output:


  • Java Program to Print Hollow Rectangle Pattern of Stars

    Java Program to Print Hollow Rectangle Pattern of Stars

    In this tutorial, we will write a program to print hollow rectangle pattern in java. Before that, you may go through the following topic in java.

    The program takes a user input for the number of rows and columns for the hollow rectangle. Then using two for loops it will print the star rectangular pattern in java.

    Example:

    Inputs:
          Rows: 6
          Columns:10
    Output:
    **********
    *        *
    *        *
    *        *
    *        *
    **********

    Java Program to Print Rectangle Star Pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, columns, i, j;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
        System.out.print("Enter the no. of columns: ");
        columns = sc.nextInt();
    
        System.out.print("Rectangular star Pattern: \n\n");
        for (i = 1; i <= rows; i++)
        {
          for (j = 1; j <= columns; j++)
          {
            if (i == 1 || i == rows || j == 1 || j == columns)
              System.out.print("*");
            else
              System.out.print(" ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Hollow rectangle star pattern in java

  • Java Program to Print Diamond Pattern

    Java Program to Print Diamond Pattern

    In this tutorial, we will write a java program to print diamond pattern of stars. Before that, you should have knowledge on the following topic in Java.


    Java Program to Print Diamond Pattern

    Source Code: the program asks the user for the input of a number of rows for the diamond star pattern in java. Then using for loop, the diamond shape pattern is displayed on the screen.

    // Print Diamond star Pattern in java
    
    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int n, j, i, space = 1;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter the no. of Rows: ");
        n = scan.nextInt();
    
        space = n - 1;
    
        for (i = 1; i <= n; i++)
        {
          for (j = 1; j <= space; j++)
          {
            System.out.print(" ");
          }
    
          space--;
          for (j = 1; j <= (2 *i - 1); j++)
          {
            System.out.print("*");
          }
    
          System.out.println();
        }
    
        space = 1;
    
        for (i = 1; i <= (n - 1); i++)
        {
          for (j = 1; j <= space; j++)
          {
            System.out.print(" ");
          }
    
          space++;
          for (j = 1; j <= (2 *(n - i) - 1); j++)
          {
            System.out.print("*");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    java diamond pattern of star

  • Java Program to Print Pyramid Pattern of Stars

    Java Program to Print Pyramid Pattern of Stars

    This article will show you how to print a simple pyramid pattern of stars in java.

    Patterns are one of the easiest ways to improve the coding skills for java. The frequent use of loops can increase your skills and printing pattern order.

    We will see two programs:

    1. To print a full pyramid by declaring the number of rows in a program.
    2. To print a full pyramid by taking user input for the number of rows.

    1. Java program to print the full pyramid pattern.

     //Pattern full pyramid
     
     public class Main
     { 
      public static void main(String[] args)  
      {  
        int rows = 6;  
        
       for (int i = 0; i < rows + 1; i++) 
       { 
        for (int j = rows; j > i; j--) 
        {  
           System.out.print(" ");  
        }  
         for (int k = 0; k < (2 * i - 1); k++) 
         {  
           System.out.print("*");  
         }  
          System.out.println();  
       }  
      }  
     }

    After the execution of the program, you will get the following result:


    2. Java program to print the full pyramid pattern (user input)

     import java.util.Scanner;
     
     public class Main
     { 
      public static void main(String[] args)  
      {  
         Scanner sc = new Scanner(System.in);
     
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt(); 
        
        for (int i = 0; i < rows + 1; i++) 
        { 
         for (int j = rows; j > i; j--) 
         {  
           System.out.print(" ");  
         }  
          for (int k = 0; k < (2 * i - 1); k++) 
          {  
            System.out.print("*");  
          }  
         System.out.println();  
       }  
      }  
     }

    After the execution of the program, you will get the following result:


  • Top 10 Different Star Pattern Programs in Java

    Top 10 Different Star Pattern Programs in Java

    This post focuses on the various Java pattern program specifically Top 10 Different Star Pattern Programs in Java. Patterns are one of the easiest ways to improve the coding skills for java. The frequent use of loops can increase your skills and printing pattern in order. This article covers various star patterns. Other are:

    Let’s begin:

    Printing Star Patterns in Java

    Pattern programs in java: Pattern 1

    Half Pyramid:
    * 
    * * 
    * * * 
    * * * * 
    * * * * * 
    * * * * * *

    Lets understand the coding.

     //Pattern half Pyramid 
    
     public class Pattern1 
     {
        public static void main(String[] args) 
        {
          int row = 6;
          System.out.println("Half Pyramid:");
    
          for(int i = 1; i <= row; ++i) 
          {
            for(int j = 1; j <= i; ++j) 
            {
              System.out.print("* ");
            }
           System.out.println();
          }
        }
     }

    Pattern programs in java: Pattern 2

    Inverted Half Pyramid:
    * * * * * * 
    * * * * * 
    * * * * 
    * * * 
    * * 
    *
    //Pattern Inverted Half Pyramid
    
    public class Pattern2 
     {
      public static void main(String[] args) 
      {
        int rows = 6;
        System.out.println("Inverted Half Pyramid:");
        for(int i = rows; i >= 1; --i) 
        {
            for(int j = 1; j <= i; ++j) 
            {
                System.out.print("* ");
            }
            System.out.println();
        }
      }
     }

    Pattern programs in java: Pattern 3

          *
         ***
        *****
       *******
      *********
     ***********
     //Pattern full pyramid
    
     public class Main
     { 
      public static void main(String[] args)  
      {  
        int rows = 6;  
        
        System.out.println();  
        
        for (int i = 0; i < rows + 1; i++) 
        { 
        for (int j = rows; j > i; j--) 
        {  
                System.out.print(" ");  
            }  
            for (int k = 0; k < (2 * i - 1); k++) 
            {  
                System.out.print("*");  
            }  
            System.out.println();  
        }  
      }  
     }

    Pattern programs in java: Pattern 4

    Enter the number of rows: 5 
    * * * * *
     * * * *
      * * *
       * *
        * 
    import java.util.Scanner;
    
    public class pattern4
    {
      public static void main(String[] args)
      {
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
    
        for (int i= 0; i<= rows-1 ; i++)
        {
            for (int j=0; j<=i; j++)
            {
                System.out.print(" ");
            }
            for (int k=0; k<=rows-1-i; k++)
            {
                System.out.print("*" + " ");
            }
            System.out.println();
        }
    
        sc.close();
    
      }
    }

    Pattern programs in java: Pattern 5

     Enter the number of rows: 5
         *
        * *
       * * *
      * * * *
     * * * * * 
      * * * * 
       * * * 
        * * 
         * 
    import java.util.Scanner;
    
    public class Pattern5
    {
      public static void main(String[] args)
      {
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
    
        for (int i= 0; i<= rows-1 ; i++)
        {
            for (int j=rows-1; j>=i; j--)
            {
                System.out.print(" ");
            }
            for (int k=0; k<=i; k++)
            {
                System.out.print("*" + " ");
            }
            System.out.println("");
        }
        for (int i= 0; i<= rows-1 ; i++)
        {
            for (int j=-1; j<=i; j++)
            {
                System.out.print(" ");
            }
            for (int k=0; k<=rows-2-i; k++)
            {
                System.out.print("*" + " ");
            }
            System.out.println("");
        }
    
        sc.close();
    
      }
    }

    Pattern programs in java: Pattern 6

    The half diamond pattern in java.

    Enter the number of rows: 5
    * 
    * * 
    * * * 
    * * * * 
    * * * * * 
    * * * * 
    * * * 
    * * 
    * 
    import java.util.Scanner;
    
    public class Pattern6
    {
      public static void main(String[] args)
      {
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
    
        for (int i= 0; i<= rows-1 ; i++)
        {
            for (int j=0; j<=i; j++) 
            { 
              System.out.print("*"+ " "); 
                
            } 
        System.out.println(""); 
            
        } 
            
        for (int i=rows-1; i>=0; i--)
        {
            for(int j=0; j <= i-1;j++)
            {
                System.out.print("*"+ " ");
            }
         System.out.println("");
        }
     
        sc.close();
    
      }
    }

    Pattern programs in java: Pattern 7

    The left half diamond pattern in java.

    Enter the number of rows: 5
        *
       **
      ***
     ****
    *****
     ****
      ***
       **
        *
    import java.util.Scanner;
    
    public class Pattern7
    {
      public static void main(String[] args)
      {
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
    
        for (int i= 1; i<= rows ; i++)
        {
            for (int j=i; j < rows ;j++)            
            {
                System.out.print(" ");
            }
            for (int k=1; k<=i;k++)
            {
                System.out.print("*");
            }
            System.out.println("");
        }
        for (int i=rows; i>=1; i--)
        {
            for(int j=i; j<=rows;j++)
            {
                System.out.print(" ");
            }
            for(int k=1; k<i ;k++) 
            {
                System.out.print("*");
            }
            System.out.println("");
    
        }
     
        sc.close();
    
      }
    }

    Pattern programs in java: Pattern 8

    Half hourglass pattern in java

    Enter the number of rows: 5
    * * * * * 
    * * * *
    * * *
    * * 
    * 
    *
    * *
    * * * 
    * * * *
    * * * * *
    import java.util.Scanner;
    
    public class Pattern8
    {
      public static void main(String[] args)
      {
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
    
        for (int i= rows-1; i>= 0; i--)
        {
            for (int j=i; j>=0; j--)
            {
                System.out.print("*"+ " ");
            }
            System.out.println("");
        }
        for (int i=0; i<= rows-1; i++)
        {
            for(int j=i; j >= 0;j--)
            {
                System.out.print("*"+ " ");
            }
            System.out.println("");
        }
     
        sc.close();
    
      }
    }

    Pattern programs in java: Pattern 9

    Hourglass pattern in java.

    Enter the number of rows: 5
    * * * * * 
     * * * *
      * * *
       * *
        *
        *
       * * 
      * * *
     * * * * 
    * * * * *
    import java.util.Scanner;
    
    public class Pattern9
    {
      public static void main(String[] args)
      {
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
    
        for (int i= 0; i<= rows-1 ; i++)
        {
            for (int j=0; j <i; j++)
            {
              System.out.print(" ");
            }
            for (int k=i; k<=rows-1; k++) 
            { 
              System.out.print("*" + " "); 
                
            } 
         System.out.println(""); 
        }
            
        for (int i= rows-1; i>= 0; i--)
        {
            for (int j=0; j< i ;j++)
            {
              System.out.print(" ");
            }
            for (int k=i; k<=rows-1; k++)
            {
              System.out.print("*" + " ");
            }
         System.out.println("");
        }
    
        sc.close();
    
      }
    }

    Pattern programs in java: Pattern 10

    Hollow triangle star pattern in java.

    import java.util.Scanner;
    
    public class Pattern10
    {
      public static void main(String[] args)
      {
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
    
        for (int i=1; i<= rows ; i++)
        {
            for (int j = i; j < rows ; j++) 
            {
                System.out.print(" ");
            }   
            for (int k = 1; k <= (2*i -1) ;k++) 
            {
                if( k==1 || i == rows || k==(2*i-1)) 
                {
                    System.out.print("*");
                }
                else 
                {
                    System.out.print(" ");
                }
            }
            System.out.println("");
        }
    
        sc.close();
    
      }
    }