Java Program to Print Half Diamond Star Pattern

Write a Java Program to print the right-side half diamond star pattern using for loop. This Java half diamond star example prints the right triangle and inverted right triangle because the combination of both gives the right-side half of the diamond.

package ShapePrograms;

import java.util.Scanner;

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

In this Java Half Diamond star pattern program, we replaced the for loop with a while loop.

package ShapePrograms;

import java.util.Scanner;

public class HalfDiamondPattern2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j;
		
		System.out.print("Enter Half Diamond Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Half Diamond Star Pattern");
		i = 1 ;
		while ( i <= rows ) 
		{
			j = 1 ;
			while (j <= i ) 
			{
				System.out.print("*");
				j++;
			}
			System.out.println();
			i++;
		}
		i = rows - 1;
		while ( i > 0) 
		{
			j = 1 ;
			while ( j <= i ) 
			{
				System.out.print("*");
				j++;
			}
			System.out.println();
			i-- ;
		}
	}
}
Enter Half Diamond Pattern Rows = 7
Printing Half Diamond Star Pattern
*
**
***
****
*****
******
*******
******
*****
****
***
**
*

Java Program to Print Half Diamond Star Pattern using do while loop

package ShapePrograms;

import java.util.Scanner;

public class HalfDiamondPattern3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j;
		
		System.out.print("Enter Half Diamond Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Half Diamond Star Pattern");
		i = 1 ;
		do
		{
			j = 1 ;
			do
			{
				System.out.print("*");
			} while (++j <= i ) ;
			System.out.println();
		} while ( ++i <= rows ) ;
		i = rows - 1;
		do
		{
			j = 1 ;
			do
			{
				System.out.print("*");
			} while ( ++j <= i ) ;
			System.out.println();
		} while ( --i > 0) ;
	}
}
Enter Half Diamond Pattern Rows = 9
Printing Half Diamond Star Pattern
*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
****
***
**
*

In this Java example, the HalfDiamondPattern function prints the half diamond pattern of a given symbol.

package ShapePrograms;

import java.util.Scanner;

public class HalfDiamondPattern4 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Half Diamond Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Enter Character for Hollow Diamond Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("Printing Half Diamond Star Pattern");
		HalfDiamondPattern(rows, ch);
	}
	
	public static void HalfDiamondPattern(int rows, char ch) {
		int i, j;
		
		for (i = 1 ; i <= rows; i++ ) 
		{
			for (j = 1 ; j <= i; j++ ) 
			{
				System.out.print(ch);	
			}
			System.out.println();
		}
		
		for (i = rows - 1 ; i > 0; i-- ) 
		{
			for (j = 1 ; j <= i; j++ ) 
			{
				System.out.print(ch);
			}
			System.out.println();
		}
	}
}
Enter Half Diamond Pattern Rows = 12
Enter Character for Hollow Diamond Pattern = #
Printing Half Diamond Star Pattern
#
##
###
####
#####
######
#######
########
#########
##########
###########
############
###########
##########
#########
########
#######
######
#####
####
###
##
#