Signup/Sign In

Java Program to Print 8 Star Pattern

In this tutorial, we will see how to print the 8-star pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use different loops to print the 8-star patterns. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.

Input: Enter the number of rows: 4

Output: The pattern is:

* *

* *

* *

* *

* *

* *

* *

Program 1: Print 8 Star Pattern

In this program, we will see how to print an 8-star pattern in java using for loop.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows.
  4. Ask the user to initialize the variable.
  5. Use a for loop to print the pattern.
  6. In the first inner loop iterate from j=1 to j=n, check the “if “condition, if it is true it will display “space'” otherwise it comes to the else part and display the “*” symbol.
  7. If the first “if” condition is false then 2nd inner for loop will be executed. In the 2nd for loop iterates from j=1 to j=n, check the “if” condition if it true it displays the “*” symbol otherwise it displays “space”.
  8. Display the result.
  9. Stop

The below example illustrates the implementation of the above algorithm.

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


Enter the number of rows: 7
*****
* *
* *
* *
* *
* *
*****
* *
* *
* *
* *
* *
*****

Program 2: Print 8 Star Pattern

In this program, we will see how to print an 8-star pattern in java using a while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare variables to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use a while loop to print the pattern.

  6. In the 1st inner while loop, if the condition at while is true then checks the “if ” condition, if the condition is true then it displays “space” otherwise it displays “*”, it will continue until the inner while loop condition is false.

  7. The 2nd inner while loop executes only the if condition at outer while loop is false, in 2nd inner while loop first checks the condition at the while will be checked after that checks the “if” condition, if condition true then displays “*” otherwise displays “space”.

  8. Display the result.

  9. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program To Print 8 Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	 
        int i=1;
        int j;
	    int k=n*2-1;
        while(i<=k)
        {
            if(i==1 || i==n || i==k)
            {
	            j=1;
                while(j<=n)
                {      
                    if(j==1  || j==n)
                    System.out.print(" ");
                    else
                    System.out.print("*");
		            j++;
               }
            }
            else
	        {
                j=1;
                while(j<=n)
                {
                    if(j==1 || j==n)
                    System.out.print("*");
                    else
                    System.out.print(" ");
                    j++;
               }
           }
	       System.out.println();
           i++;
        } 
    }
}


Enter the number of rows: 8
******
* *
* *
* *
* *
* *
* *
******
* *
* *
* *
* *
* *
* *
******

Program 3: Java Program to Print 8 Star Pattern

In this program, we will see how to print an 8-star pattern in java using a do-while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare variables to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use a do-while loop to print the pattern.

  6. In the 1st inner do-while loop if the “if” condition is true it will display space otherwise it will display “*”, 2nd inner do-while loop if the “if” condition is true then it will display “*” otherwise it will display space.

  7. The total code in the outer do-while loop will be executed until the while condition is false i.e while(i<=k).

  8. Display the result.

  9. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program To Print 8 Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	 
        int i=1;
        int j;
	    int k=n*2-1;
        do
        {
            if(i==1 || i==n || i==k)
            {
	            j=1;
                do
                {      
                    if(j==1  || j==n)
                    System.out.print(" ");
                    else
                    System.out.print("*");
		            j++;
               }while(j<=n);
            }
        else
	    {
               j=1;
               do
               {
                    if(j==1 || j==n)
                    System.out.print("*");
                    else
                    System.out.print(" ");
                    j++;
                }while(j<=n);
        }
	    System.out.println();
        i++;
        }while(i<=k);            
    }
}


Enter the number of rows: 6
****
* *
* *
* *
* *
****
* *
* *
* *
* *
****



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.