Program to print hollow Triangle pattern
Last Updated :
13 Mar, 2023
Given the number of lines as N, the task is to form the given hollow Triangle pattern.
Examples:
Input: N = 6
Output:
************
***** *****
**** ****
*** ***
** **
* *
Approach:
- Input number of rows to print from the user as N.
- To iterate through rows run an outer loop from number of rows till it is greater than 1. The loop structure should look like for(i=N; i>=1; i–).
- To print spaces, run an inner loop from i to space (another local variable). The loop structure should look like for(k=1; k=1; j–); .Inside this loop print star.
- After printing all columns of a row, move to next line i.e. print new line.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void pattern( int N)
{
int i, j, k = 0, space = 1, rows = N;
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
cout << "*" ;
}
if (i != rows) {
for (k = 1; k <= space; k++) {
cout << " " ;
}
space = space + 2;
}
for (j = i; j >= 1; j--) {
if (j != rows)
cout << "*" ;
}
cout << "\n" ;
}
cout << "\n" ;
}
int main()
{
int N = 6;
pattern(N);
return 0;
}
|
C
#include <stdio.h>
void pattern( int N)
{
int i, j, k = 0, space = 1, rows = N;
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf ( "*" );
}
if (i != rows) {
for (k = 1; k <= space; k++) {
printf ( " " );
}
space = space + 2;
}
for (j = i; j >= 1; j--) {
if (j != rows)
printf ( "*" );
}
printf ( "\n" );
}
printf ( "\n" );
}
int main()
{
int N = 6;
pattern(N);
return 0;
}
|
Java
import java.util.*;
class solution
{
static void pattern( int N)
{
int i, j, k = 0 , space = 1 , rows = N;
for (i = rows; i >= 1 ; i--) {
for (j = 1 ; j <= i; j++) {
System.out.print( "*" );
}
if (i != rows) {
for (k = 1 ; k <= space; k++) {
System.out.print( " " );
}
space = space + 2 ;
}
for (j = i; j >= 1 ; j--) {
if (j != rows)
System.out.print( "*" );
}
System.out.print( "\n" );
}
System.out.print( "\n" );
}
public static void main(String args[])
{
int N = 6 ;
pattern(N);
}
}
|
Python3
def pattern(N):
k, space, rows = 0 , 1 , N
for i in range (rows, 0 , - 1 ):
for j in range ( 1 , i + 1 ):
print ( '*' , end = '')
if i ! = rows:
for k in range ( 1 , space + 1 ):
print ( ' ' , end = '')
space + = 2
for j in range (i, 0 , - 1 ):
if j ! = rows:
print ( '*' , end = '')
print ()
print ()
N = 6
pattern(N)
|
C#
using System;
class GFG
{
static void pattern( int N)
{
int i, j, k = 0, space = 1, rows = N;
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
Console.WriteLine( "*" );
}
if (i != rows)
{
for (k = 1; k <= space; k++)
{
Console.Write( " " );
}
space = space + 2;
}
for (j = i; j >= 1; j--)
{
if (j != rows)
Console.Write( "*" );
}
Console.Write( "\n" );
}
Console.Write( "\n" );
}
public static void Main()
{
int N = 6;
pattern(N);
}
}
|
PHP
<?php
function pattern( $N )
{
$k = 0;
$space = 1;
$rows = $N ;
for ( $i = $rows ; $i >= 1; $i --)
{
for ( $j = 1; $j <= $i ; $j ++)
{
echo "*" ;
}
if ( $i != $rows )
{
for ( $k = 1; $k <= $space ; $k ++)
{
echo " " ;
}
$space = $space + 2;
}
for ( $j = $i ; $j >= 1; $j --)
{
if ( $j != $rows )
echo "*" ;
}
echo "\n" ;
}
echo "\n" ;
}
$N = 6;
pattern( $N );
?>
|
Javascript
<script>
function pattern(N) {
var i,
j,
k = 0,
space = 1,
rows = N;
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
document.write( "*" );
}
if (i != rows) {
for (k = 1; k <= space; k++) {
document.write( " " );
}
space = space + 2;
}
for (j = i; j >= 1; j--) {
if (j != rows) document.write( "*" );
}
document.write( "<br>" );
}
document.write( "<br>" );
}
var N = 6;
pattern(N);
</script>
|
Output:
***********
***** *****
**** ****
*** ***
** **
* *
Time complexity: O(n2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...