 Open in App
Not now

# Program to print a Hollow Triangle inside a Triangle

• Difficulty Level : Basic
• Last Updated : 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

 `// C++ implementation of the above approach` `#include ``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`

## Javascript

 ``

Output:

```                *
*   *
*       *
*     *     *
*     * * *     *
*                   *
*                       *
*                           *
* * * * * * * * * * * * * * * * *```

Time Complexity: O(n2)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up