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
#include <bits/stdc++.h>
using namespace std;
void printPattern( int n)
{
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j < 2 * n; j++) {
if (j == (n - i + 1)
|| j == (n + i - 1)) {
cout << "* " ;
}
else if ((i >= 4 && i <= n - 4)
&& (j == n - i + 4
|| j == n + i - 4)) {
cout << "* " ;
}
else if (i == n
|| (i == n - 4
&& j >= n - (n - 2 * 4)
&& j <= n + n - 2 * 4)) {
cout << "* " ;
}
else {
cout << " "
<< " " ;
}
}
cout << "\n" ;
}
}
int main()
{
int N = 9;
printPattern(N);
}
|
Java
import java.util.*;
class GFG{
static void printPattern( int n)
{
int i, j;
for (i = 1 ; i <= n; i++) {
for (j = 1 ; j < 2 * n; j++) {
if (j == (n - i + 1 )
|| j == (n + i - 1 )) {
System.out.print( "* " );
}
else if ((i >= 4 && i <= n - 4 )
&& (j == n - i + 4
|| j == n + i - 4 )) {
System.out.print( "* " );
}
else if (i == n
|| (i == n - 4
&& j >= n - (n - 2 * 4 )
&& j <= n + n - 2 * 4 )) {
System.out.print( "* " );
}
else {
System.out.print( " "
+ " " );
}
}
System.out.print( "\n" );
}
}
public static void main(String[] args)
{
int N = 9 ;
printPattern(N);
}
}
|
Python3
def printPattern(n):
for i in range ( 1 , n + 1 ):
for j in range ( 1 , 2 * n):
if (j = = (n - i + 1 )
or j = = (n + i - 1 )):
print ( "* " ,end = "")
elif ((i > = 4 and i < = n - 4 )
and (j = = n - i + 4
or j = = n + i - 4 )):
print ( "* " ,end = "")
elif (i = = n
or (i = = n - 4
and j > = n - (n - 2 * 4 )
and j < = n + n - 2 * 4 )):
print ( "* " , end = "")
else :
print ( " " + " " , end = "")
print ()
N = 9
printPattern(N)
|
C#
using System;
class GFG{
static void printPattern( int n)
{
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j < 2 * n; j++) {
if (j == (n - i + 1)
|| j == (n + i - 1)) {
Console.Write( "* " );
}
else if ((i >= 4 && i <= n - 4)
&& (j == n - i + 4
|| j == n + i - 4)) {
Console.Write( "* " );
}
else if (i == n
|| (i == n - 4
&& j >= n - (n - 2 * 4)
&& j <= n + n - 2 * 4)) {
Console.Write( "* " );
}
else {
Console.Write( " "
+ " " );
}
}
Console.Write( "\n" );
}
}
public static void Main(String[] args)
{
int N = 9;
printPattern(N);
}
}
|
Javascript
<script>
function printPattern(n) {
var i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j < 2 * n; j++) {
if (j == n - i + 1 || j == n + i - 1) {
document.write( "* " );
}
else if (
i >= 4 &&
i <= n - 4 &&
(j == n - i + 4 || j == n + i - 4)
) {
document.write( "* " );
}
else if (
i == n ||
(i == n - 4 && j >= n - (n - 2 * 4) && j <= n + n - 2 * 4)
) {
document.write( "* " );
}
else {
document.write( " " );
document.write( " " );
}
}
document.write( "<br>" );
}
}
var N = 9;
printPattern(N);
</script>
|
Output:
*
* *
* *
* * *
* * * * *
* *
* *
* *
* * * * * * * * * * * * * * * * *
Time Complexity: O(n2)
Auxiliary Space: O(1)
Last Updated :
24 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...