Related Articles

Related Articles

Program to print half diamond Number-Star pattern
  • Last Updated : 25 Sep, 2019

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print half diamond
// number star pattern
#include <iostream>
using namespace std;
  
// Function to print the given pattern
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) {
                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 code
int main()
{
    int n;
      
    n = 6;
      
    printPattern(n);
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print half 
// diamond number star pattern
class GFG
{
// Function to print
// the given pattern
static 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 code
public static void main(String [] args)
{
    int n;
      
    n = 6;
      
    printPattern(n);
}
}
  
// This code is contributed
// by Smitha

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to
# print half diamond 
# number star pattern 
  
# Function to print the 
# given pattern 
def 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 Code
if __name__ == "__main__" :
  
    n = 6
  
    printPattern(n)
              
# This code is contributed
# by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print half diamond
// number star pattern
  
using System;
  
public class GFG
{
// Function to print the given pattern
static 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 code
public static void Main()
{
    int n;
       
    n = 6;
       
    printPattern(n);
       
}
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print half 
// diamond number star pattern
  
// Function to print the
// given pattern
function 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 
?>

chevron_right


Output:

3*3*3
2*2
1
1
2*2
3*3*3



My Personal Notes arrow_drop_up
Recommended Articles
Page :