Tag: java star pattern

  • Inverted Hollow Pyramid using Stars in Java

    Inverted Hollow Pyramid using Stars in Java

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

    Both of the programs below take the user input for the number of rows and display the result using the for loop in java. The program is basically the inverted hollow triangle star pattern in java program.


    Inverted hollow pyramid star pattern in java

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

    Output:

    Inverted hollow pyramid star pattern in java

  • Half Hourglass Star Pattern in Java

    Half Hourglass Star Pattern in Java

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

    The program below takes a user input for the number of rows needed and then in decreasing order of the first half is printed and then the increasing order of the second half of the half part of sandglass is printed.

    This is a half hourglass pattern program in java or you can say half sandglass pattern program using a star.


    Half Hourglass Star Pattern in Java

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

    Output:

    java half hourglass pattern

  • Hollow Inverted Right Triangle Star Pattern in Java

    Hollow Inverted Right Triangle Star Pattern in Java

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

    We will display the hollow inverted right triangle pattern of stars in java using different loops. We will use for loop and while and write two different codes.

    The programs however take the number of rows as an input from the user and print the hollow pattern. There is a use of if..else statement in the program to check the boolean condition. It is placed inside the for loop and while loop.


    Hollow Inverted Right Triangle Star Pattern in Java

    Using For Loop

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

    Output:

    Hollow Inverted Right Triangle Star Pattern in Java

    Using While Loop

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

    Output:

    Hollow Inverted Right Triangle Star Pattern in Java

  • Java Program to print Hollow Rectangle or Square star patterns

    Java Program to print Hollow Rectangle or Square star patterns

    In this tutorial, we will write a program to display a hollow pattern of stars in java. We will write code for two different shapes patterns. Before that, you may go through the following topic in java.

    Hollow rectangle star pattern 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 Hollow 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

    Hollow square star pattern in java

    As the square contains only sides and both length and breadth are equal so the program only takes user input for a number of rows and prints the same number of rows and columns.

    Example:

    Inputs:
       Rows: 7
    Output:
    *******
    *     *
    *     *
    *     *
    *     *
    *     *
    *******

    Java program to print hollow square pattern of stars

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

    Output:

    Hollow square star pattern in java

  • Hollow Diamond Pattern in Java

    Hollow Diamond Pattern in Java

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

    The program takes a user input for the number of rows for the hollow pattern using the Scanner class. We will use a for loop to iterate and print the pattern.

    The user can give the input number as they wish and get the hollow diamond star pattern according to their input provided.


    Java Program To Display Hollow Diamond Star Pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int rows, i, j;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        //Upper part of diamond
        for (i = 1; i <= rows; i++)
        {
          for (j = rows; j > i; j--)
            System.out.print(" ");
    
          System.out.print("*");
    
          for (j = 1; j < (i - 1) *2; j++)
            System.out.print(" ");
    
          if (i == 1)
            System.out.print("\n");
          else
            System.out.print("*\n");
        }
    
        //lower part of diamond
        for (i = rows - 1; i >= 1; i--)
        {
          for (j = rows; j > i; j--)
            System.out.print(" ");
    
          System.out.print("*");
    
          for (j = 1; j < (i - 1) *2; j++)
            System.out.print(" ");
    
          if (i == 1)
            System.out.print("\n");
          else
            System.out.print("*\n");
        }
      }
    }

    Output:

    Hollow Diamond Pattern in Java

    Print Hollow Diamond Pattern using Star in Java using while loop

    The program below takes the rows input from the user as the above program did but here we will produce a hollow diamond pattern using a while loop instead of for loop.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int rows, i, j;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        //Upper part of diamond
        i = 1;
        while (i <= rows)
        {
          j = rows;
          while (j > i)
          {
            System.out.print(" ");
            j--;
          }
          System.out.print("*");
    
          j = 1;
          while (j < (i - 1) *2)
          {
            System.out.print(" ");
            j++;
          }
    
          if (i == 1)
            System.out.print("\n");
          else
            System.out.print("*\n");
    
          i++;
        }
    
        //lower part of diamond
        i = rows - 1;
        while (i >= 1)
        {
          j = rows;
          while (j > i)
          {
            System.out.print(" ");
            j--;
          }
    
          System.out.print("*");
    
          j = 1;
          while (j < (i - 1) *2)
          {
            System.out.print(" ");
            j++;
          }
    
          if (i == 1)
            System.out.print("\n");
          else
            System.out.print("*\n");
    
          i--;
        }
      }
    }

    The above program with a while loop will produce the same result as the for loop has produced. The only difference is we used while instead of for loop.


  • Right Arrow Star Pattern in Java

    Right Arrow Star Pattern in Java

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


    Right Arrow Star Pattern in Java

    This is a program on the arrowhead pattern in java. The program takes a user input for the number of rows in a pattern and using for loop, it displays the pattern.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, i, j, k;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        System.out.print("Output:\n");
    
        //upper part of the arrow head
        for (i = 1; i < rows; i++)
        {
          for (j = 0; j < i; j++)
            System.out.print(" ");
    
          for (k = 0; k < i; k++)
            System.out.print("*");
    
          System.out.println();
        }
    
        //lower part of the arrow head
        for (i = 0; i < rows; i++)
        {
          for (j = 0; j < rows - i; j++)
            System.out.print(" ");
    
          for (k = 0; k < rows - i; k++)
            System.out.print("*");
    
          System.out.println();
        }
      }
    }

    Output:

    Right Arrow Star Pattern in Java

    User Input for Character

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, i, j, k;
        char symbol;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
        System.out.print("Enter the symbol for the pattern: ");
        symbol = sc.next().charAt(0);
    
        System.out.print("Output:\n");
    
        //upper part of the arrow head
        for (i = 1; i < rows; i++)
        {
          for (j = 0; j < i; j++)
            System.out.print(" ");
    
          for (k = 0; k < i; k++)
            System.out.print(symbol);
    
          System.out.println();
        }
    
        //lower part of the arrow head
        for (i = 0; i < rows; i++)
        {
          for (j = 0; j < rows - i; j++)
            System.out.print(" ");
    
          for (k = 0; k < rows - i; k++)
            System.out.print(symbol);
    
          System.out.println();
        }
      }
    }

    Output:

    Right Arrow Star Pattern in Java 2

  • Hourglass Star Pattern in Java

    Hourglass Star Pattern in Java

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

    The program below takes a user input for the number of rows needed and then in decreasing order of the first half is printed and then the increasing order of the second half of the sandglass is printed.

    This is an hourglass pattern program in java or you can say sandglass pattern program using a star.

    Input of row: 4  
    * * * *
     * * * 
      * * 
       *  
       * 
      * * 
     * * * 
    * * * *

    Hourglass Star Pattern in Java

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

    Output:

    Hourglass Star Pattern in Java

  • Hollow Pyramid using Stars in Java

    Hollow Pyramid using Stars in Java

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

    Both of the programs below take the user input for the number of rows and display the result using the for loop in java. The program is basically the hollow triangle star pattern in java program.


    Hollow Pyramid using Stars in Java

    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 n = sc.nextInt();
    
        for (int i = 1; i <= n; i++)
        {
          for (int j = 1; j <= n - i; j++)
          {
            System.out.print(" ");
          }
    
          if (i == 1 || i == n)
          {
            for (int j = 1; j <= i *2 - 1; j++)
            {
              System.out.print("*");
            }
          }
          else
          {
            for (int j = 1; j <= i *2 - 1; j++)
            {
              if (j == 1 || j == i *2 - 1)
                System.out.print("*");
              else
                System.out.print(" ");
            }
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Hollow Pyramid using Stars in Java

  • Java program to print Inverted Equilateral Star Triangle

    Java program to print Inverted Equilateral Star Triangle

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

    It is a Java Program to Print Reverse Pyramid Star Pattern or you can say upside-down equilateral triangle pattern in java.

    The program takes the user input for the number of rows to be taken to print the reverse equilateral star pattern.

    Example:

    Input: number = 7   
    Output:
     *************  
     ***********   
      *********    
       *******     
        *****     
         ***       
          * 

    Java program to print Inverted Equilateral Star Triangle

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

    Output:

    Inverted Equilateral Star Triangle

  • Java Program to Print Left Triangle Star Pattern

    Java Program to Print Left Triangle Star Pattern

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


    Java Program to Print Left Triangle Star Pattern

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

    Output:

    left triangle star pattern