FOR FREE YEAR SOLVED

Inverted Hollow Right Star Triangle

Back to Programming

Description

Inverted Hollow Right Star Triangle

In this pattern, the number of lines is taken as input. Now two for loops are used here to print the pattern. The outer for loop here is used to count the number of lines. Hence, the loop is executing from n to 1. The loop is written in reverse order because here the number of stars is decreased. the inner loop is used to print the stars so it runs from 1 to i (the line number in decreasing order). The stars are printed only on the boundary lines which makes the pattern hollow.

Algorithm

INPUT: the number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: [taking the inputs]
	Read n [number of lines]
Step 2: [printing the pattern]
	For i=n to 1 repeat
		For j=1 to i repeat
			If i=1 or i=n or j=1 or j=i 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=n;i>=1;i--)-------------------------------------- n

                {

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

                                {

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

                                                                printf("*");

                                                else

                                                                printf(" ");           

                                }

                                printf("\n");

                }

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