Program to print hollow Triangle pattern

28 Jun, 2022
Given the number of lines as N, the task is to form the given hollow Triangle pattern.Examples:  
Input: N = 6
Output:
************
*****  *****
****    ****
***      ***
**        **
*          *
Approach: 
Input number of rows to print from the user as N.To iterate through rows run an outer loop from number of rows till it is greater than 1. The loop structure should look like for(i=N; i>=1; i–).To print spaces, run an inner loop from i to space (another local variable). The loop structure should look like for(k=1; k=1; j–); .Inside this loop print star.After printing all columns of a row, move to next line i.e. print new line.
Input number of rows to print from the user as N.
To iterate through rows run an outer loop from number of rows till it is greater than 1. The loop structure should look like for(i=N; i>=1; i–).
To print spaces, run an inner loop from i to space (another local variable). The loop structure should look like for(k=1; k=1; j–); .Inside this loop print star.
After printing all columns of a row, move to next line i.e. print new line.
Below is the implementation of the above approach: 
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program for printing// the hollow triangle pattern #include <iostream>using namespace std; // Function for printing patternvoid pattern(int N){    int i, j, k = 0, space = 1, rows = N;     // For printing stars    for (i = rows; i >= 1; i--) {        for (j = 1; j <= i; j++) {            cout << "*";        }        if (i != rows) {            // for printing space            for (k = 1; k <= space; k++) {                cout << " ";            }             // increment by 2            space = space + 2;        }        for (j = i; j >= 1; j--) {            if (j != rows)                cout << "*";        }        cout << "\n";    }    cout << "\n";} // Driver codeint main(){     // Get N    int N = 6;     // Print the pattern    pattern(N);     return 0;}
// C program for printing// the hollow triangle pattern #include <stdio.h> // Function for printing patternvoid pattern(int N){    int i, j, k = 0, space = 1, rows = N;     // For printing stars    for (i = rows; i >= 1; i--) {        for (j = 1; j <= i; j++) {            printf("*");        }        if (i != rows) {            // for printing space            for (k = 1; k <= space; k++) {                printf(" ");            }             // increment by 2            space = space + 2;        }        for (j = i; j >= 1; j--) {            if (j != rows)                printf("*");        }        printf("\n");    }    printf("\n");} // Driver codeint main(){     // Get N    int N = 6;     // Print the pattern    pattern(N);     return 0;}
// Java program for printing// the hollow triangle patternimport java.util.*; class solution{ // Function for printing patternstatic void pattern(int N){    int i, j, k = 0, space = 1, rows = N;     // For printing stars    for (i = rows; i >= 1; i--) {        for (j = 1; j <= i; j++) {            System.out.print("*");        }        if (i != rows) {            // for printing space            for (k = 1; k <= space; k++) {            System.out.print(" ");            }             // increment by 2            space = space + 2;        }        for (j = i; j >= 1; j--) {            if (j != rows)                System.out.print("*");        }        System.out.print("\n");    }    System.out.print("\n");} // Driver codepublic static void main(String args[]){     // Get N    int N = 6;     // Print the pattern    pattern(N); }} //This code is contributed by Surendra_Gangwar
# Python 3 program for printing# the hollow triangle pattern # Function for printing patterndef pattern(N):    k, space, rows = 0, 1, N     # For printing stars    for i in range(rows, 0, -1):        for j in range(1, i + 1):            print('*', end = '')         if i != rows:            # for printing space            for k in range(1, space + 1):                print(' ', end = '')             # increment by 2            space += 2        for j in range(i, 0, -1):            if j != rows:                print('*', end = '')        print()    print() # Driver Code # Get NN = 6 # Print the patternpattern(N) # This code is contributed by# SamyuktaSHegde
// C# program for printing// the hollow triangle patternusing System; class GFG{ // Function for printing patternstatic void pattern(int N){    int i, j, k = 0, space = 1, rows = N;     // For printing stars    for (i = rows; i >= 1; i--)    {        for (j = 1; j <= i; j++)        {            Console.WriteLine("*");        }        if (i != rows)        {            // for printing space            for (k = 1; k <= space; k++)            {            Console.Write(" ");            }             // increment by 2            space = space + 2;        }        for (j = i; j >= 1; j--)        {            if (j != rows)                Console.Write("*");        }        Console.Write("\n");    }    Console.Write("\n");} // Driver codepublic static void Main(){     // Get N    int N = 6;     // Print the pattern    pattern(N);}} // This code is contributed// by Rajput-Ji
<?php// PHP program for printing// the hollow triangle pattern // Function for printing patternfunction pattern($N){    $k = 0;    $space = 1;    $rows = $N;     // For printing stars    for ($i = $rows; $i >= 1; $i--)    {        for ($j = 1; $j <= $i; $j++)        {            echo "*";        }        if ($i != $rows)        {            // for printing space            for ($k = 1; $k <= $space; $k++)            {                echo " ";            }             // increment by 2            $space = $space + 2;        }        for ($j = $i; $j >= 1; $j--)        {            if ($j != $rows)                echo "*";        }        echo "\n";    }    echo "\n";} // Driver code // Get N$N = 6; // Print the patternpattern($N); // This code is contributed by// Archana_kumari?>
<script>      // JavaScript program for printing      // the hollow triangle pattern       // Function for printing pattern      function pattern(N) {        var i,          j,          k = 0,          space = 1,          rows = N;         // For printing stars        for (i = rows; i >= 1; i--) {          for (j = 1; j <= i; j++) {            document.write("*");          }          if (i != rows) {            // for printing space            for (k = 1; k <= space; k++) {              document.write("  ");            }             // increment by 2            space = space + 2;          }          for (j = i; j >= 1; j--) {            if (j != rows) document.write("*");          }          document.write("<br>");        }        document.write("<br>");      }       // Driver code      // Get N      var N = 6;             // Print the pattern      pattern(N);             // This code is contributed by rdtank.    </script>
***********
***** *****
****   ****
***     ***
**       **
*         *
 
Time complexity: O(n2)
Auxiliary Space: O(1)
SURENDRA_GANGWAR
Rajput-Ji
SamyuktaSHegde
archana_kumari
rdtank
krishnav4
pattern-printing
C Programs
C++ Programs
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Header files in C/C++ and its uses
C Program to read contents of Whole File
How to return multiple values from a function in C or C++?
C++ Program to check Prime Number
C Program to Swap two Numbers
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
How to return multiple values from a function in C or C++?
Shallow Copy and Deep Copy in C++
C++ program for hashing with chaining