Java Program to Print Alphabet K Pattern

In this Java pattern program, we show the steps to print the stars in an Alphabet K 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();

int n = rows / 2;
for (int i = 0; i < rows; i++)
{
System.out.printf("*");
for (int j = 0; j <= n; j++)
{
if (j == Math.abs(n - i))
{
System.out.printf("*");
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
}
}
Enter Rows = 15
*       *
*      * 
*     *  
*    *   
*   *    
*  *     
* *      
**       
* *      
*  *     
*   *    
*    *   
*     *  
*      * 
*       *

Within this Alphabet K 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, n;

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

n = rows / 2;
i = 0 ;
while ( i < rows )
{
System.out.printf("*");
j = 0;
while ( j <= n)
{
if (j == Math.abs(n - i))
{
System.out.printf("*");
}
else
{
System.out.printf(" ");
}
j++;
}
System.out.println();
i++;
}
}
}
Enter Rows = 18
*         *
*        * 
*       *  
*      *   
*     *    
*    *     
*   *      
*  *       
* *        
**         
* *        
*  *       
*   *      
*    *     
*     *    
*      *   
*       *  
*        * 

In this program, we created an AlphabetKShape function to print the Alphabet L 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);

AlphabetKShape(rows, a);
}

public static void AlphabetKShape(int rows, char a)
{
int n = rows/ 2;

for (int i = 0; i < rows; i++)
{
System.out.printf("%c", a);
for (int j = 0; j <= n; j++)
{
if ( j == Math.abs(n - i))
{
System.out.printf("%c", a);
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
}
}
Print Alphabet K Pattern