Java Program to Print Alphabet S Pattern

In this Java pattern program, we show the steps to print the stars in an Alphabet S shape or pattern using for loop, while loop, and functions.

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);

System.out.print("Enter Rows = ");
int rows = sc.nextInt();

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if ((i == 0 || i == rows / 2 || i == rows - 1) && (j != 0) && (j != rows - 1))
{
System.out.printf("* ");
}
else if ((i != 0) && (j == 0) && (i < rows / 2 ))
{
System.out.printf("* ");
}
else if(j == rows - 1 && i > rows / 2 && i != rows - 1)
{
System.out.printf("* ");
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
}
}
Enter Rows = 15
  * * * * * * * * * * * * *   
*                             
*                             
*                             
*                             
*                             
*                             
  * * * * * * * * * * * * *   
                            * 
                            * 
                            * 
                            * 
                            * 
                            * 
  * * * * * * * * * * * * *   

Within this Alphabet S shape or Pattern program, we replaced the for loop with a while loop to print the stars at each position. For more Alphabet Patterns and star pattern programs, please use the hyperlinks.

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);
int rows, i, j;

System.out.print("Enter Rows = ");
rows = sc.nextInt();

i = 0;
while ( i < rows)
{
j = 0;
while ( j < rows)
{
if (i == 0 || i == rows / 2 || i == rows - 1)
{
System.out.printf("* ");
}
else if (((j == 0) && (i < rows / 2 )) || (j == rows - 1 && i > rows / 2))
{
System.out.printf("* ");
}
else
{
System.out.printf(" ");
}
j++;
}
System.out.println();
i++;
}
}
}
Enter Rows = 16
* * * * * * * * * * * * * * * * 
*                               
*                               
*                               
*                               
*                               
*                               
*                               
* * * * * * * * * * * * * * * * 
                              * 
                              * 
                              * 
                              * 
                              * 
                              * 
* * * * * * * * * * * * * * * * 

In this program, we created an AlphabetSShape function to print the Alphabet S Pattern or shape filled with stars on rows and columns.

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);

System.out.print("Enter Rows = ");
int rows = sc.nextInt();

System.out.print("Enter Character = ");
char a = sc.next().charAt(0);

AlphabetSShape(rows, a);
}

public static void AlphabetSShape(int rows, char a)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if ((i == 0 || i == rows / 2 || i == rows - 1) && (j != 0) && (j != rows - 1))
{
System.out.printf("%c ", a);
}
else if ((i != 0) && (j == 0) && (i < rows / 2 ))
{
System.out.printf("%c ", a);
}
else if(j == rows - 1 && i > rows / 2 && i != rows - 1)
{
System.out.printf("%c ", a);
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
}
}
Print Alphabet S Pattern