Program to print a Hollow Triangle inside a Triangle

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the pattern
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)) {
                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 Code
int main()
{
    int N = 9;
  
    printPattern(N);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
import java.util.*;
  
class GFG{
   
// Function to print the pattern
static 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 Code
public static void main(String[] args)
{
    int N = 9;
   
    printPattern(N);
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach
  
# Function to prthe pattern
def 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 Code
N = 9
  
printPattern(N)
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG{
    
// Function to print the pattern
static 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 Code
public static void Main(String[] args)
{
    int N = 9;
    
    printPattern(N);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

                *                 
              *   *               
            *       *             
          *     *     *           
        *     * * *     *         
      *                   *       
    *                       *     
  *                           *   
* * * * * * * * * * * * * * * * *

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.