Given a number N(≥ 8), the task is to print a Hollow Triangle inside a Triangle pattern.
Example:
Input: N = 9
Output:
*
* *
* *
* * *
* * * * *
* *
* *
* *
* * * * * * * * * * * * * * * * *
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
// 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); } |
Java
// 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 |
Python3
# Python3 implementation of the above approach # Function to print the 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 |
C#
// 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 |
*
* *
* *
* * *
* * * * *
* *
* *
* *
* * * * * * * * * * * * * * * * *
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program to print hollow Triangle pattern
- Solid square inside a hollow square | Pattern
- Program to print solid and hollow square patterns
- Program to print solid and hollow rhombus patterns
- Program to print hollow pyramid and diamond pattern
- Program to print hollow rectangle or square star patterns
- Program to print the hollow numerical parallelogram
- Program to print the pattern with two hollow Triangles
- Program to Print Mirrored Hollow Parallelogram
- Python Program to print hollow half diamond hash pattern
- Hollow lower triangular pattern
- Minimum number of blocks required to form Hollow Rectangular Prism
- Program to print Square inside a Square
- Check whether a given point lies inside a triangle or not
- Count Integral points inside a Triangle
- Area of a triangle inside a parallelogram
- C Program to print Floyd's triangle
- Program to print Sum Triangle for a given array
- Program to print binary right angle triangle
- Program to print Fibonacci Triangle
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.


