Given an integer N, the task is to print half-diamond-star pattern.
*
**
***
****
*****
******
*****
****
***
**
*
Examples:
Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** *
Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print then separately with the help of the loops. The key observation for printing the upper half and lower half is described as below:
- Upper half: The upper half of the pattern contains star ‘*’ in increasing order where ith line contains following number of star:
Number of '*' in ith line =
- Lower Half: The lower half of the pattern contains star ‘*’ in decreasing order where ith line contains following number of star:
Number of '*' in ith line =
Below is the implementation of the above approach:
C++
// C++ implementation to print the// half diamond star pattern#include <iostream>usingnamespacestd;// Function to print the// half diamond star patternvoidhalfDiamondStar(intN){inti, j;// Loop to print the upper half// diamond patternfor(i = 0; i < N; i++) {for(j = 0; j < i + 1; j++)cout <<"*";cout <<"\n";}// Loop to print the lower half// diamond patternfor(i = 1; i < N; i++) {for(j = i; j < N; j++)cout <<"*";cout <<"\n";}}// Driver Codeintmain(){intN = 5;// Function CallhalfDiamondStar(N);}Java
// Java implementation to print the// half diamond star patternimportjava.util.*;classGFG{// Function to print the// half diamond star patternstaticvoidhalfDiamondStar(intN){inti, j;// Loop to print the upper half// diamond patternfor(i =0; i < N; i++){for(j =0; j < i +1; j++)System.out.print("*");System.out.print("\n");}// Loop to print the lower half// diamond patternfor(i =1; i < N; i++){for(j = i; j < N; j++)System.out.print("*");System.out.print("\n");}}// Driver Codepublicstaticvoidmain(String[] args){intN =5;// Function CallhalfDiamondStar(N);}}// This code is contributed by Rohit_ranjanPython3
# Python3 implementation to print the# half diamond star pattern# Function to print the# half diamond star patterndefhalfDiamondStar(N):# Loop to print the upper half# diamond patternforiinrange(N):forjinrange(0, i+1):print("*", end="")print()# Loop to print the lower half# diamond patternforiinrange(1, N):forjinrange(i, N):print("*", end="")print()# Driver CodeN=5;# Function CallhalfDiamondStar(N);# This code is contributed by skylagsC#
// C# implementation to print the// half diamond star patternusingSystem;classGFG{// Function to print the// half diamond star patternstaticvoidhalfDiamondStar(intN){inti, j;// Loop to print the upper half// diamond patternfor(i = 0; i < N; i++){for(j = 0; j < i + 1; j++)Console.Write("*");Console.Write("\n");}// Loop to print the lower half// diamond patternfor(i = 1; i < N; i++){for(j = i; j < N; j++)Console.Write("*");Console.Write("\n");}}// Driver CodepublicstaticvoidMain(String[] args){intN = 5;// Function CallhalfDiamondStar(N);}}// This code is contributed by Rohit_ranjanOutput:* ** *** **** ***** **** *** ** *
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


