FOR FREE MATERIALS

Hollow Star Right Triangle

Back to Programming

Description

Hollow Star Right Triangle

Here in this pattern printing, the number of lines of the pattern is taken as the input. Two for loops are used to display the pattern. The first for loop (the outer for loop) is used to count the line number so the loop is from 1 to n. here the stars are printed only for the boundary positions. That is for each line i(except the last line), the star is printed at the 1st position and at the ith position. For the last line i.e. the nth line n numbers of stars are printed.

Algorithm

INPUT: the number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: read n [the number of lines]
Step 2: for i=1 to n repeat
		For j=1 to i repeat
			If j=1 or j=i or i=n then
				Print ‘*’
			Else
				Print “ “
			[End of ‘if’]
		[End of ‘for’ loop]
		Move to the next line
	[End of ‘for’ loop]
Step 3: stop.

Code

Time Complexity:

for(i=1;i<=n;i++)----------------------------------------- n

                {        for(j=1;j<=i;j++)-------------------------------- i

                                {

                                                if(j==1||j==i||i==n)

                                                                printf("*");

                                                else

                                                                printf(" ");                           

                                }

                                printf("\n");

                }

 

The complexity is: O(n*i)=O(n2).