FOR FREE MATERIALS

Hollow Star Left Triangle

Back to Programming

Description

Hollow  Star Left Triangle

 

The number of lines is taken as input here. The outer for loop is used to count the line number of the pattern. There are two inner for loops, the first of which is used to print the spaces. If the line number is i, then there are n-i spaces before the stars. So the loop executes from 1 to n-i for each i. here the stars are printed only for the boundary positions. That is for each line i(except the first and the last line), the star is printed at the 1st position and at the ith position. For the 1st line the star is printed at ith position. For the last line i.e. the nth line n number of stars are printed.

Algorithm

INPUT: number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: [taking the input]
	Read n [number of lines]
Step 2: [printing the pattern]
	For i = 1 to n repeat
		For j=1 to n-i repeat
			Print “ “
		[End of ‘for’ loop]
		For k=1 to i repeat
			If k=1 or k=i or i=n repeat
				Print “*”
			Else
				Print “ “
		[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

                {        //printing the spaces

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

                                                printf(" ");

                                //printing the starts

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

                                {

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

                                                                printf("*");

                                                else

                                                                printf(" ");

                                }

                                printf("\n"); }

 

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