Java Program to Print H Star Pattern

Write a Java program to print H star pattern using for loop.

package Shapes3;

import java.util.Scanner;

public class HStarPattern1 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k, l;
		
		System.out.print("Please Enter H Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing H Star Pattern");
		
		for (i = 1; i <= rows; i++ ) 
		{
			for (j = 1; j <= i; j++ ) 
			{
				System.out.print("*");
			}
			for(k = i * 2; k <= rows * 2 - 1; k++) 
			{
				System.out.print(" ");
			}
			for (l = 1; l <= i; l++ ) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
		
		for (i = 1; i <= rows - 1; i++ ) 
		{
			for (j = rows - 1; j >= i; j-- ) 
			{
				System.out.print("*");
			}
			for(k = 1; k <= i * 2; k++) 
			{
				System.out.print(" ");
			}
			for (l = rows - 1; l >= i; l-- ) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
Java Program to Print H Star Pattern

This Java program displays the alphabet H pattern of stars using a while loop.

package Shapes3;

import java.util.Scanner;

public class HStarPattern2 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k, l;
		
		System.out.print("Please Enter H Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing H Star Pattern");
		i = 1;
		while( i <= rows ) 
		{
			j = 1;
			while(j <= i) 
			{
				System.out.print("*");
				j++;
			}
			k = i * 2;
			while( k <= rows * 2 - 1) 
			{
				System.out.print(" ");
				k++;
			}
			l = 1;
			while( l <= i ) 
			{
				System.out.print("*");
				l++;
			}
			System.out.println();
			i++;
		}
		
		i = 1;
		while (i <= rows - 1 ) 
		{
			j = rows - 1;
			while ( j >= i ) 
			{
				System.out.print("*");
				j--;
			}
			k = 1;
			while( k <= i * 2) 
			{
				System.out.print(" ");
				k++;
			}
			l = rows - 1; 
			while (l >= i ) 
			{
				System.out.print("*");
				l--;
			}
			System.out.println();
			i++;
		}
	}
}
Please Enter H Pattern Rows = 11
Printing H Star Pattern
*                    *
**                  **
***                ***
****              ****
*****            *****
******          ******
*******        *******
********      ********
*********    *********
**********  **********
**********************
**********  **********
*********    *********
********      ********
*******        *******
******          ******
*****            *****
****              ****
***                ***
**                  **
*                    *

This Java Program uses the do while loop to print the H pattern of stars.

package Shapes3;

import java.util.Scanner;

public class HStarPattern3 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k, l;
		
		System.out.print("Please Enter H Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing H Star Pattern");
		i = 1;
		do
		{
			j = 1;
			do
			{
				System.out.print("*");
			} while(++j <= i);
			k = i * 2;
			while( k <= rows * 2 - 1) 
			{
				System.out.print(" ");
				k++;
			}
			l = 1;
			do
			{
				System.out.print("*");
			} while( ++l <= i );
			System.out.println();
		} while( ++i <= rows ) ;
		
		i = 1;
		do
		{
			j = rows - 1;
			do
			{
				System.out.print("*");
			} while (--j >= i );
			k = 1;
			do
			{
				System.out.print(" ");
			} while(++k <= i * 2);
			l = rows - 1; 
			do 
			{
				System.out.print("*");
			} while (--l >= i );
			System.out.println();
		} while (++i <= rows - 1 );
	
	}
}
Please Enter H Pattern Rows = 13
Printing H Star Pattern
*                        *
**                      **
***                    ***
****                  ****
*****                *****
******              ******
*******            *******
********          ********
*********        *********
**********      **********
***********    ***********
************  ************
**************************
************  ************
***********    ***********
**********      **********
*********        *********
********          ********
*******            *******
******              ******
*****                *****
****                  ****
***                    ***
**                      **
*                        *

In this Java example, HPattern function allows user to enter the character and prints the H pattern of the given character.

package Shapes3;

import java.util.Scanner;

public class HStarPattern4 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter H Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Enter Character for H Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("Printing H Pattern");
		HPattern(rows, ch);
		
	}
	public static void HPattern(int rows, char ch) {
		int i, j, k, l;
		
		for (i = 1; i <= rows; i++ ) 
		{
			for (j = 1; j <= i; j++ ) 
			{
				System.out.print(ch);
			}
			for(k = i * 2; k <= rows * 2 - 1; k++) 
			{
				System.out.print(" ");
			}
			for (l = 1; l <= i; l++ ) 
			{
				System.out.print(ch);
			}
			System.out.println();
		}
		
		for (i = 1; i <= rows - 1; i++ ) 
		{
			for (j = rows - 1; j >= i; j-- ) 
			{
				System.out.print(ch);
			}
			for(k = 1; k <= i * 2; k++) 
			{
				System.out.print(" ");
			}
			for (l = rows - 1; l >= i; l-- ) 
			{
				System.out.print(ch);
			}
			System.out.println();
		}
	}
}
Please Enter H Pattern Rows = 17
Enter Character for H Pattern = $
Printing H Pattern
$                                $
$$                              $$
$$$                            $$$
$$$$                          $$$$
$$$$$                        $$$$$
$$$$$$                      $$$$$$
$$$$$$$                    $$$$$$$
$$$$$$$$                  $$$$$$$$
$$$$$$$$$                $$$$$$$$$
$$$$$$$$$$              $$$$$$$$$$
$$$$$$$$$$$            $$$$$$$$$$$
$$$$$$$$$$$$          $$$$$$$$$$$$
$$$$$$$$$$$$$        $$$$$$$$$$$$$
$$$$$$$$$$$$$$      $$$$$$$$$$$$$$
$$$$$$$$$$$$$$$    $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$  $$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$  $$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$    $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$      $$$$$$$$$$$$$$
$$$$$$$$$$$$$        $$$$$$$$$$$$$
$$$$$$$$$$$$          $$$$$$$$$$$$
$$$$$$$$$$$            $$$$$$$$$$$
$$$$$$$$$$              $$$$$$$$$$
$$$$$$$$$                $$$$$$$$$
$$$$$$$$                  $$$$$$$$
$$$$$$$                    $$$$$$$
$$$$$$                      $$$$$$
$$$$$                        $$$$$
$$$$                          $$$$
$$$                            $$$
$$                              $$
$                                $