Open In App

Program to print half Diamond star pattern

Last Updated : 13 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 them 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 = i
  • 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 = N - i

C++

// C++ implementation to print the
// half diamond star pattern
  
#include <iostream>
  
using namespace std;
  
// Function to print the
// half diamond star pattern
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++)
            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 Code
int main()
{
    int N = 5;
  
    // Function Call
    halfDiamondStar(N);
}

                    

Java

// Java implementation to print the
// half diamond star pattern
import java.util.*;
  
class GFG{
  
// Function to print the
// half diamond star pattern
static 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 Code
public static void main(String[] args)
{
    int N = 5;
  
    // Function Call
    halfDiamondStar(N);
}
}
  
// This code is contributed by Rohit_ranjan

                    

Python3

# 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#

// C# implementation to print the
// half diamond star pattern
using System;
  
class GFG{
  
// Function to print the
// half diamond star pattern
static 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 Code
public static void Main(String[] args)
{
    int N = 5;
  
    // Function Call
    halfDiamondStar(N);
}
}
  
// This code is contributed by Rohit_ranjan

                    

Javascript

// JavaScript implementation to print the
// half diamond star pattern
  
// Function to print the
// half diamond star pattern
function halfDiamondStar(N)
{
    let i, j;
  
    // Loop to print the upper half
    // diamond pattern
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < i + 1; j++)
            console.log("*");
            console.log("<br>");
             console.log("\n");
    }
  
    // Loop to print the lower half
    // diamond pattern
    for (i = 1; i < N; i++)
    {
        for (j = i; j < N; j++)
            console.log("*");
            console.log("<br>");
        console.log("\n");
          
    }
      
}
  
// Driver Code
    let N = 5;
  
    // Function Call
    halfDiamondStar(N);

                    

Output
*
**
***
****
*****
****
***
**
*

Time complexity: O(N2) where N is given integer
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Program to print half diamond Number-Star pattern
Given a number N which represents the number of rows. The task is to print a half diamond Number-Star pattern as shown in the below examples.Note: N is always an even number.Examples: Input: N = 4 Output: 2*2 1 1 2*2 Input: N = 6 Output: 3*3*3 2*2 1 1 2*2 3*3*3 On carefully observing the above pattern, it can be broken down into two different trian
6 min read
Python program to display half diamond pattern of numbers with star border
Given a number n, the task is to write a Python program to print a half-diamond pattern of numbers with a star border. Examples: Input: n = 5 Output: * *1* *121* *12321* *1234321* *123454321* *1234321* *12321* *121* *1* * Input: n = 3 Output: * *1* *121* *12321* *121* *1* * Approach: Two for loops will be run in this program in order to print the n
2 min read
Program to Print Inverted Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is is to print a left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and so forth until the Nth row, which contains only 1 star. All the stars are aligned to the right. Examples: Input: 3Output: *** ** * Input: 5Output: ***** **** *** ** * Approach
4 min read
Program to Print Inverted Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Output:****** Approach: The problem can be solved us
3 min read
Program to Print Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of right half pyramid pattern. In right half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are left aligned. Examples: Input: 3Output: ****** Input: 5Output: *************** Approach: The problem can be solved using two nested loops. T
3 min read
Program to Print Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print N rows of left half pyramid pattern. In left half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are right aligned. Examples: Input: 3Output: * ***** Input: 5Output: * ** *** ********* Approach: The problem can be solved using t
4 min read
Java Program to Print Diamond Shape Star Pattern
In this article, we are going to learn how to print diamond shape star patterns in Java. Illustration: Input: number = 7 Output: * *** ***** ******* ********* *********** ************* *********** ********* ******* ***** *** * Methods: When it comes to pattern printing we do opt for standard ways of printing them via loops only. We will try out dif
6 min read
C++ Program To Print Hollow Star Pyramid Diamond Shape Pattern
Here, we will build a C++ program to print the hollow star pyramid diamond shape pattern that can be achieved with two approaches i.e. Using for LoopUsing while loop Input: n = 5 Output: * * * * * * * * * * * * * * * *1. Using for loop C/C++ Code // C++ program to print hollow diamond pattern #include &lt;iostream&gt; using namespace std; int main(
3 min read
Python Program to print hollow half diamond hash pattern
Give an integer N and the task is to print hollow half diamond pattern. Examples: Input : 6 Output : # # # # # # # # # # # # # # # # # # # # Input : 7 Output : # # # # # # # # # # # # # # # # # # # # # # # # Approach: The idea is to break the pattern into two parts: Upper part: For the upper half start the for loop with iterator (i) from 1 to n and
4 min read
C Program For Printing Simple Half Right Star Pyramid Pattern
Here, we will develop a C Program To Print Simple Half Right Star Pyramid Pattern using two approaches i.e. Using for loop Using while loop Input: rows = 5 Output: * * * * * * * * * * * * * * * 1. Using for loop: First for loop is used to identify the number of rows and the second for loop is used to identify the number of columns. Here the values
2 min read
three90RightbarBannerImg