Hollow Star Pyramid
To print this pattern, the number of lines is taken as the input from the user. Three for loops (one outer for loop and two inners for loops) are used here to print the pattern. The outer for loop is used to count the line number. For ith line the number of spaces before printing the star is n-i. so the first inner for loop used to print the spaces executes from 1 to n-i for each line i. the stars are printed on the boundary line. a separate variable is used to count the number of stars for a line. so, the 2nd inner for loop is used to print the star of each line.
INPUT: number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: [taking the input]
Read n [number of lines]
Step 2: [printing the pattern]
Set st<-1
For i=1 to n repeat
For j=1 to n-i repeat
Print " "
[End of ‘for’ loop]
For k=1 to st repeat
if k=1 or k=st or i=1 or i=n
Print “*"
Else
Print “ “
[End of ‘for’ loop]
Move to the next line
Set st=st+2
[End of ‘for’ loop]
Step 3: Stop
for(i=1;i<=n;i++)--------------------------------------- n
{ //printing the spaces
for(j=1;j<=n-i;j++)--------------------------- n-i
printf(" ");
//printing the stars
for(k=1;k<=st;k++)--------------------------- st
{
if(i==1||i==n||k==1||k==st)
printf("*");
else
printf(" ");
}
printf("\n");
st+=2; }
The complexity is: O(n*(n-i+st))=O().