FOR FREE CONTENT

Inverted Pyramid Number Pattern

Back to Programming

Description

Inverted Pyramid Number Pattern:

Here in this pattern printing, the number of lines of the pattern is taken as the input. Three 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 n to 1 (here the number of lines are counted in reversed order). The second for loop is used to print the spaces before the elements at each line. for ith line the numbers are printed from 1 to i.

Algorithm

INPUT: the number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: read n [the number of lines]
Step 2: for i=n to 1 repeat
		For k=1 to n-i repeat
			Print “ “
		[End of ‘for’ loop]
		For j=1 to i repeat
			Print j
		[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(k=1;k<=n-i;k++)------------------------ n-i

                                                printf(" ");

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

                                                printf("%d ",j);

                                printf("\n");

                }

 

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