FOR FREE CONTENT

Inverted Half Right Star Pyramid

Back to Programming

Description

Inverted Half Pyramid

*****

****

***

**

*

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 are decreased. the inner loop is used to print the stars so it runs from 1 to i (the line number in decreasing order)

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
			Print “*”
		[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

                                                printf("*");

                                printf("\n");

                }

The first ‘for’ loop is running from n to 1 and the inner for loop is running from 1 to i. so the time complexity is O(n*i)=O(n).