Write a Java program to print 8 star pattern using for loop.
package Shapes3;
import java.util.Scanner;
public class Star8Pattern1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter 8 Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing 8 Pattern of Stars");
for (int i = 1; i <= rows * 2 - 1; i++ )
{
if(i == 1 || i == rows || i == rows * 2 - 1)
{
for (int j = 1; j <= rows; j++ )
{
if(j == 1 || j == rows)
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}
}
else
{
for (int k = 1; k <= rows; k++ )
{
if(k == 1 || k == rows)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
}
System.out.println();
}
}
}

Java Program to Print 8 Star Pattern using a while loop
package Shapes3;
import java.util.Scanner;
public class Star8Pattern2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter 8 Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing 8 Pattern of Stars");
int i = 1;
while( i <= rows * 2 - 1 )
{
if(i == 1 || i == rows || i == rows * 2 - 1)
{
int j = 1;
while( j <= rows )
{
if(j == 1 || j == rows)
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
j++;
}
}
else
{
int k = 1;
while( k <= rows )
{
if(k == 1 || k == rows)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
k++;
}
}
System.out.println();
i++;
}
}
}
Please Enter 8 Pattern Rows = 7
Printing 8 Pattern of Stars
*****
* *
* *
* *
* *
* *
*****
* *
* *
* *
* *
* *
*****
This program uses the do while loop to print the 8 digit star pattern.
package Shapes3;
import java.util.Scanner;
public class Star8Pattern3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter 8 Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing 8 Pattern of Stars");
int i = 1;
do
{
if(i == 1 || i == rows || i == rows * 2 - 1)
{
int j = 1;
do
{
if(j == 1 || j == rows)
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
} while( ++j <= rows );
}
else
{
int k = 1;
do
{
if(k == 1 || k == rows)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
} while( ++k <= rows );
}
System.out.println();
} while( ++i <= rows * 2 - 1 );
}
}
Please Enter 8 Pattern Rows = 10
Printing 8 Pattern of Stars
********
* *
* *
* *
* *
* *
* *
* *
* *
********
* *
* *
* *
* *
* *
* *
* *
* *
********
In this example, Pattern8 function allows rows and character and prints the digit 8 pattern of a given character.
package Shapes3;
import java.util.Scanner;
public class Star8Pattern4 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter 8 Pattern Rows = ");
int rows = sc.nextInt();
System.out.print("Enter Character for 8 Pattern = ");
char ch = sc.next().charAt(0);
System.out.println("Printing 8 Pattern of Stars");
Pattern8(rows, ch);
}
public static void Pattern8(int rows, char ch)
{
for (int i = 1; i <= rows * 2 - 1; i++ )
{
if(i == 1 || i == rows || i == rows * 2 - 1)
{
for (int j = 1; j <= rows; j++ )
{
if(j == 1 || j == rows)
{
System.out.print(" ");
}
else
{
System.out.print(ch);
}
}
}
else
{
for (int k = 1; k <= rows; k++ )
{
if(k == 1 || k == rows)
{
System.out.print(ch);
}
else
{
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
Please Enter 8 Pattern Rows = 13
Enter Character for 8 Pattern = #
Printing 8 Pattern of Stars
###########
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
###########
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
###########