Program to print a Hollow Triangle inside a Triangle

24 Nov, 2021
Given a number N(≥ 8), the task is to print a Hollow Triangle inside a Triangle pattern.Example:  
Input: N = 9
Output:
                *                 
              *   *               
            *       *             
          *     *     *           
        *     * * *     *         
      *                   *       
    *                       *     
  *                           *   
* * * * * * * * * * * * * * * * * 
 
Approach:  
Let i be the index for rows and j be the index for columns. Then:  
For sides of outer triangle: If the index of column(j) is equals to (N – i + 1) or (N + i – 1), then ‘*’ is printed for equal sides of outer triangle.  
if(j == (N - i + 1)
   || j == (N + i - 1) {
  print('*')
}
 
For sides of inner triangle: If the (index of row(i) is less than (N – 4) and greater than (4) and index of column(j) is equals to (N – i + 4) or (N + i + 4), then ‘*’ is printed for equal sides of inner triangle.  
if(  (i >= 4 
     && i <= n - 4) 
  && (j == N - i + 4 
     || j == N + i - 4) ) {
    print('*')
}
 
For bases of the outer triangle: If the index of row(i) is equal to N, then ‘*’ is printed for the base of outer triangle.  
if(i == N) {
   print('*')
}
 
For bases of the inner triangle: If the index of row(i) is equals (N – 4) and the column index(j) must be greater than equals to (N – (N – 2*4)), and j is less than equals to (N + N – 2*4), then ‘*’ is printed for the base of inner triangle.  
if( (i == N - 4) 
 && (j >= N - (N - 2 * 4) ) 
 && (j <= n + n - 2 * 4) ) ) {
   print('*')
}
 
Below is the implementation of the above approach:  
CPP
Java
Python3
C#
Javascript
// C++ implementation of the above approach #include <bits/stdc++.h>using namespace std; // Function to print the patternvoid printPattern(int n){     int i, j;     // Loop for rows    for (i = 1; i <= n; i++) {         // Loop for column        for (j = 1; j < 2 * n; j++) {             // For printing equal sides            // of outer triangle            if (j == (n - i + 1)                || j == (n + i - 1)) {                cout << "* ";            }             // For printing equal sides            // of inner triangle            else if ((i >= 4 && i <= n - 4)                     && (j == n - i + 4                         || j == n + i - 4)) {                 cout << "* ";            }             // For printing base            // of both triangle            else if (i == n                     || (i == n - 4                         && j >= n - (n - 2 * 4)                         && j <= n + n - 2 * 4)) {                 cout << "* ";            }             // For spacing between the triangle            else {                cout << " "                     << " ";            }        }        cout << "\n";    }} // Driver Codeint main(){    int N = 9;     printPattern(N);}
// Java implementation of the above approachimport java.util.*; class GFG{  // Function to print the patternstatic void printPattern(int n){      int i, j;      // Loop for rows    for (i = 1; i <= n; i++) {          // Loop for column        for (j = 1; j < 2 * n; j++) {              // For printing equal sides            // of outer triangle            if (j == (n - i + 1)                || j == (n + i - 1)) {                System.out.print("* ");            }              // For printing equal sides            // of inner triangle            else if ((i >= 4 && i <= n - 4)                     && (j == n - i + 4                         || j == n + i - 4)) {                  System.out.print("* ");            }              // For printing base            // of both triangle            else if (i == n                     || (i == n - 4                         && j >= n - (n - 2 * 4)                         && j <= n + n - 2 * 4)) {                  System.out.print("* ");            }              // For spacing between the triangle            else {                System.out.print(" "                    + " ");            }        }        System.out.print("\n");    }}  // Driver Codepublic static void main(String[] args){    int N = 9;      printPattern(N);}} // This code is contributed by sapnasingh4991
# Python3 implementation of the above approach # Function to print the patterndef printPattern(n):     # Loop for rows    for i in range(1, n + 1):         # Loop for column        for j in range(1, 2 * n):             # For printing equal sides            # of outer triangle            if (j == (n - i + 1)                or j == (n + i - 1)):                print("* ",end="")             # For printing equal sides            # of inner triangle            elif ((i >= 4 and i <= n - 4)                    and (j == n - i + 4                        or j == n + i - 4)):                 print("* ",end="")             # For printing base            # of both triangle            elif (i == n                    or (i == n - 4                        and j >= n - (n - 2 * 4)                        and j <= n + n - 2 * 4)):                 print("* ", end="")             # For spacing between the triangle            else :                print(" "+" ", end="")         print() # Driver CodeN = 9 printPattern(N) # This code is contributed by mohit kumar 29
// C# implementation of the above approachusing System; class GFG{   // Function to print the patternstatic void printPattern(int n){       int i, j;       // Loop for rows    for (i = 1; i <= n; i++) {           // Loop for column        for (j = 1; j < 2 * n; j++) {               // For printing equal sides            // of outer triangle            if (j == (n - i + 1)                || j == (n + i - 1)) {                Console.Write("* ");            }               // For printing equal sides            // of inner triangle            else if ((i >= 4 && i <= n - 4)                     && (j == n - i + 4                         || j == n + i - 4)) {                   Console.Write("* ");            }               // For printing base            // of both triangle            else if (i == n                     || (i == n - 4                         && j >= n - (n - 2 * 4)                         && j <= n + n - 2 * 4)) {                   Console.Write("* ");            }               // For spacing between the triangle            else {                Console.Write(" "                    + " ");            }        }        Console.Write("\n");    }}   // Driver Codepublic static void Main(String[] args){    int N = 9;       printPattern(N);}} // This code is contributed by 29AjayKumar
<script>       // JavaScript implementation of the above approach       // Function to print the pattern      function printPattern(n) {        var i, j;         // Loop for rows        for (i = 1; i <= n; i++) {          // Loop for column          for (j = 1; j < 2 * n; j++) {            // For printing equal sides            // of outer triangle            if (j == n - i + 1 || j == n + i - 1) {              document.write("* ");            }             // For printing equal sides            // of inner triangle            else if (              i >= 4 &&              i <= n - 4 &&              (j == n - i + 4 || j == n + i - 4)            ) {              document.write("* ");            }             // For printing base            // of both triangle            else if (              i == n ||              (i == n - 4 && j >= n - (n - 2 * 4) && j <= n + n - 2 * 4)            ) {              document.write("* ");            }             // For spacing between the triangle            else {              document.write(" ");              document.write("  ");            }          }          document.write("<br>");        }      }       // Driver Code       var N = 9;       printPattern(N);           </script>
                *                 
              *   *               
            *       *             
          *     *     *           
        *     * * *     *         
      *                   *       
    *                       *     
  *                           *   
* * * * * * * * * * * * * * * * *
 
Time Complexity: O(n2)
Auxiliary Space: O(1)
mohit kumar 29
sapnasingh4991
29AjayKumar
Code_Mech
rdtank
subham348
pattern-printing
Mathematical
Pattern Searching
School Programming
Mathematical
pattern-printing
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.