Left Triangle Star Pattern
*
**
***
****
The number of lines is taken as input here. The outer for loop is used to count the line number of the pattern. There are two inner for loops, the first of which is used to print the spaces. If the line number is i, then there are n-i spaces before the stars. So the loop executes from 1 to n-i for each i. And for ith line there are i number of stars. So the 2nd inner for loop is executing from 1 to i.
INPUT: number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: [taking the input]
Read n [number of lines]
Step 2: [printing the pattern]
For i = 1 to n repeat
For j=1 to n-i repeat
Print “ “
[End of ‘for’ loop]
For k=1 to i repeat
Print “*”
[End of ‘for’ loop]
Move to the next line
[End of ‘for’ loop]
Step 3: Stop.
for(i=1;i<=n;i++)---------------------------------------------------------------------------O(n)
{ //printing the spaces
for(j=1;j<=n-i;j++)--------------------------------------------------------------O(n-i)
printf(" ");
//printing the starts
for(k=1;k<=i;k++)--------------------------------------------------------------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 n-i and the second loop is from 1 to i. so the time complexity is O(n*(n-i+i))=O(n2).