FOR FREE YEAR SOLVED

Inverted Hollow Right Number Triangle

Back to Programming

Description

Inverted Hollow Right Number 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. the stars are printed on the boundary line which makes the pattern hollow.

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=i to n repeat
			If i=1 then
				Print j
			Else if j=i then
				Print i
			Else if j=n then
				Print n
			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=i;j<=n;j++)---------------------------------------- n-i+1

                                {              if(i==1)

                                                                printf("%d ",j);

                                                else if(j==i)

                                                                printf("%d ",i);

                                                else if(j==n)

                                                                printf("%d ",n);

                                                else

                                                                printf("  ");       }

                                printf("\n");

                }

 

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

 

Contributed by