Program to print half Diamond star pattern - GeeksforGeeks

31 Dec, 2020
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 = 

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++JavaPython3C#C++// C++ implementation to print the// half diamond star pattern  #include <iostream>  using namespace std;  // Function to print the// half diamond star patternvoid halfDiamondStar(int N){    int i, j;      // Loop to print the upper half    // diamond pattern    for (i = 0; i < N; i++) {        for (j = 0; j < i + 1; j++)            cout << "*";        cout << "\n";    }      // Loop to print the lower half    // diamond pattern    for (i = 1; i < N; i++) {        for (j = i; j < N; j++)            cout << "*";        cout << "\n";    }}  // Driver Codeint main(){    int N = 5;      // Function Call    halfDiamondStar(N);}Java// Java implementation to print the// half diamond star patternimport java.util.*;  class GFG{  // Function to print the// half diamond star patternstatic void halfDiamondStar(int N){    int i, j;      // Loop to print the upper half    // diamond pattern    for (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 pattern    for (i = 1; i < N; i++)     {        for (j = i; j < N; j++)             System.out.print("*");        System.out.print("\n");    }}  // Driver Codepublic static void main(String[] args){    int N = 5;      // Function Call    halfDiamondStar(N);}}  // This code is contributed by Rohit_ranjanPython3# Python3 implementation to print the # half diamond star pattern   # Function to print the # half diamond star pattern def halfDiamondStar(N):          # Loop to print the upper half     # diamond pattern     for i in range(N):          for j in range(0, i + 1):            print("*", end = "")        print()      # Loop to print the lower half     # diamond pattern     for i in range(1, N):          for j in range(i, N):            print("*", end = "")        print()  # Driver Code N = 5;   # Function Call halfDiamondStar(N);  # This code is contributed by skylagsC#// C# implementation to print the// half diamond star patternusing System;  class GFG{  // Function to print the// half diamond star patternstatic void halfDiamondStar(int N){    int i, j;      // Loop to print the upper half    // diamond pattern    for(i = 0; i < N; i++)    {       for(j = 0; j < i + 1; j++)           Console.Write("*");       Console.Write("\n");    }      // Loop to print the lower half    // diamond pattern    for(i = 1; i < N; i++)     {       for(j = i; j < N; j++)           Console.Write("*");       Console.Write("\n");    }}  // Driver Codepublic static void Main(String[] args){    int N = 5;      // Function Call    halfDiamondStar(N);}}  // This code is contributed by Rohit_ranjanOutput:*
**
***
****
*****
****
***
**
*
My Personal Notes
arrow_drop_upSave
Number of '*' in ith line = 

Below is the implementation of the above approach:
C++
Java
Python3
C#
// C++ implementation to print the// half diamond star pattern  #include <iostream>  using namespace std;  // Function to print the// half diamond star patternvoid halfDiamondStar(int N){    int i, j;      // Loop to print the upper half    // diamond pattern    for (i = 0; i < N; i++) {        for (j = 0; j < i + 1; j++)            cout << "*";        cout << "\n";    }      // Loop to print the lower half    // diamond pattern    for (i = 1; i < N; i++) {        for (j = i; j < N; j++)            cout << "*";        cout << "\n";    }}  // Driver Codeint main(){    int N = 5;      // Function Call    halfDiamondStar(N);}
// Java implementation to print the// half diamond star patternimport java.util.*;  class GFG{  // Function to print the// half diamond star patternstatic void halfDiamondStar(int N){    int i, j;      // Loop to print the upper half    // diamond pattern    for (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 pattern    for (i = 1; i < N; i++)     {        for (j = i; j < N; j++)             System.out.print("*");        System.out.print("\n");    }}  // Driver Codepublic static void main(String[] args){    int N = 5;      // Function Call    halfDiamondStar(N);}}  // This code is contributed by Rohit_ranjan
# Python3 implementation to print the # half diamond star pattern   # Function to print the # half diamond star pattern def halfDiamondStar(N):          # Loop to print the upper half     # diamond pattern     for i in range(N):          for j in range(0, i + 1):            print("*", end = "")        print()      # Loop to print the lower half     # diamond pattern     for i in range(1, N):          for j in range(i, N):            print("*", end = "")        print()  # Driver Code N = 5;   # Function Call halfDiamondStar(N);  # This code is contributed by skylags
// C# implementation to print the// half diamond star patternusing System;  class GFG{  // Function to print the// half diamond star patternstatic void halfDiamondStar(int N){    int i, j;      // Loop to print the upper half    // diamond pattern    for(i = 0; i < N; i++)    {       for(j = 0; j < i + 1; j++)           Console.Write("*");       Console.Write("\n");    }      // Loop to print the lower half    // diamond pattern    for(i = 1; i < N; i++)     {       for(j = i; j < N; j++)           Console.Write("*");       Console.Write("\n");    }}  // Driver Codepublic static void Main(String[] args){    int N = 5;      // Function Call    halfDiamondStar(N);}}  // This code is contributed by Rohit_ranjan
*
**
***
****
*****
****
***
**
*

Rohit_ranjan
skylags
pattern-printing
Python Pattern-printing
C Language
C#
C++
Java
Programming Language
Python
School Programming
pattern-printing
Java
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
Left Shift and Right Shift Operators in C/C++
fork() in C
Command line arguments in C/C++
Difference between Abstract Class and Interface in C#
C# | How to check whether a List contains a specified element
C# | IsNullOrEmpty() Method
String.Split() Method in C# with Examples
C# | Arrays of Strings