Java Program to Print Alphabet B Pattern

In this article, we will show the list of Java programs that print the star pattern of the alphabet from A to Z (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z) using the for loop, while loop, and functions with an example.

Java Program to Print Alphabet B Pattern

In this Java pattern program, we show the steps to print the stars in an Alphabet B 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 Numbers of Rows = ");
int rows = sc.nextInt();

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

Within this Alphabet B shape or Pattern program in Python, we replaced the Python for loop syntax with a Python while loop to print the stars at each position.

For more pattern programs, please refer to the Java Alphabet Pattern programs and Java 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 Numbers of Rows = ");
rows = sc.nextInt();

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

In this program, we created an AlphabetBShape function to print the Alphabet B Pattern or shape filled with stars on rows and columns. For more Java Alphabet Pattern programs and Java 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);

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

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

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

Also Read