FOR FREE CONTENT

Right Triangle Star Pattern

Back to Programming

Description

Right Triangle Star Pattern

*

**

***

****

*****

Here in this Right Triangle Star 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. for ith line the number of stars is also i. so the inner for loop here is used to print the star at each line. the loop is from 1 to i to print the i number of stars for the ith line.

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=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=1;i<=n;i++)------------------------------------------------------------------O(n)

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

                                         printf("*");

                                printf("\n");

                }

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