Program to print half diamond Number-Star pattern

06 Jun, 2022
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 triangles:  
An upside down right angled triangle.
A normal right angled triangle.
For the first triangle pattern:  
Run two loops, first will take care of the number of rows and other will take care of the ‘*’ and numbers being printed.
Inside the loops check whether the iterations of inner loop has reached to the given number of rows or not.
If yes, print only number, otherwise print ‘*’ and number both.
The same approach will be followed for the second triangle pattern with a slight change that here the condition will be checked where the inner iteration is equal to 0 or not.Below is the implementation of above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to print half diamond// number star pattern#include <iostream>using namespace std; // Function to print the given patternvoid printPattern(int r){    r = r / 2;    int count = r;     // loop to print upper half pattern    for (int i = 0; i < r; i++) {        for (int j = r; j > i; j--) {            if (j != r) {                cout << "*" << count;            }            else {                cout << count;            }        }        count--;        cout << endl;    }    count++;     // loop to print lower half pattern    for (int i = 0; i < r; i++) {        for (int j = 0; j <= i; j++) {            if (j != 0) {                cout << "*" << count;            }            else {                cout << count;            }        }        count++;        cout << endl;    }} // Driver codeint main(){    int n;         n = 6;         printPattern(n);         return 0;}
// Java program to print half// diamond number star patternclass GFG{// Function to print// the given patternstatic void printPattern(int r){    r = r / 2;    int count = r;     // loop to print    // upper half pattern    for (int i = 0; i < r; i++)    {        for (int j = r; j > i; j--)        {            if (j != r)            {                System.out.print("*" + count);            }            else            {                System.out.print(count);            }        }        count--;        System.out.print("\n");    }    count++;     // loop to print lower    // half pattern    for (int i = 0; i < r; i++)    {        for (int j = 0; j <= i; j++)        {            if (j != 0)            {                System.out.print("*" + count);            }            else            {                System.out.print(count);            }        }        count++;        System.out.print("\n");    }} // Driver codepublic static void main(String [] args){    int n;         n = 6;         printPattern(n);}} // This code is contributed// by Smitha
# Python 3 program to# print half diamond# number star pattern # Function to print the# given patterndef printPattern(r) :     r = r // 2    count = r     # loop to print upper    # half pattern    for i in range(r) :        for j in range(r, i, -1) :            if j != r :                print("*" + str(count),                             end = "")            else :                print(count, end = "")         count -= 1        print()     count += 1     # loop to print lower    # half pattern    for i in range(r) :        for j in range(i + 1):            if j != 0 :                print("*" + str(count),                             end = "")            else :                print(count, end = "")         count += 1        print() # Driver Codeif __name__ == "__main__" :     n = 6     printPattern(n)             # This code is contributed# by ANKITRAI1
// C++ program to print half diamond// number star pattern using System; public class GFG{// Function to print the given patternstatic void printPattern(int r){    r = r / 2;    int count = r;      // loop to print upper half pattern    for (int i = 0; i < r; i++) {        for (int j = r; j > i; j--) {            if (j != r) {                Console.Write("*" + count);            }            else {                Console.Write( count);            }        }        count--;        Console.WriteLine();    }    count++;      // loop to print lower half pattern    for (int i = 0; i < r; i++) {        for (int j = 0; j <= i; j++) {            if (j != 0) {                Console.Write("*" + count);            }            else {                Console.Write( count);            }        }        count++;        Console.WriteLine();    }}  // Driver codepublic static void Main(){    int n;          n = 6;          printPattern(n);      }}
<?php// PHP program to print half// diamond number star pattern // Function to print the// given patternfunction printPattern($r){    $r = $r / 2;    $count = $r;     // loop to print upper half pattern    for ($i = 0; $i < $r; $i++)    {        for ($j = $r; $j > $i; $j--)        {            if ($j != $r)            {                echo "*" . $count;            }            else            {                echo $count;            }        }        $count--;        echo "\n";    }    $count++;     // loop to print lower half pattern    for ($i = 0; $i < $r; $i++)    {        for ($j = 0; $j <= $i; $j++)        {            if ($j != 0)            {                echo "*" . $count;            }            else            {                echo $count;            }        }        $count++;        echo "\n";    }} // Driver code$n = 6; printPattern($n); // This code is contributed// by Arnab Kundu?>
<script>       // JavaScript program to print half diamond      // number star pattern       // Function to print the given pattern      function printPattern(r) {        r = r / 2;        var count = parseInt(r);         // loop to print upper half pattern        for (var i = 0; i < r; i++) {          for (var j = r; j > i; j--) {            if (j != r) {              document.write("*" + count);            } else {              document.write(count);            }          }          count--;          document.write("<br>");        }        count++;         // loop to print lower half pattern        for (var i = 0; i < r; i++) {          for (var j = 0; j <= i; j++) {            if (j != 0) {              document.write("*" + count);            } else {              document.write(count);            }          }          count++;          document.write("<br>");        }      }       // Driver code      var n;      n = 6;      printPattern(n);       </script>
3*3*3
2*2
1
1
2*2
3*3*3
 
Time Complexity: O(r2), where r represents the given input.Auxiliary Space: O(1), no extra space is required, so it is a constant.
Smitha Dinesh Semwal
tufan_gupta2000
andrew1234
ankthon
prataher
rdtank
tamanna17122007
pattern-printing
C++ Programs
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Shallow Copy and Deep Copy in C++
C++ Program to check if a given String is Palindrome or not
How to find the minimum and maximum element of a Vector using STL in C++?
C++ Program for QuickSort
C Program to Swap two Numbers
cin in C++
Passing a function as a parameter in C++
Const keyword in C++
Enumerated Types or Enums in C++
Program to implement Singly Linked List in C++ using class