Open In App

Print the pattern by using one loop | Set 2 (Using Continue Statement)

Last Updated : 16 Feb, 2023
Comments
Improve
Suggest changes
29 Likes
Like
Report

Given a number n, print triangular pattern. We are allowed to use only one loop.
Example: 

Input: 7
Output:
*
* * 
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *

We use single for-loop and in the loop we maintain two variables for line count and current star count. If current star count is less than current line count, we print a star and continue. Else we print a new line and increment line count.  

C++
Java Python 3 C# PHP JavaScript

Output: 

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *

Time Complexity: O(n2)
Auxiliary Space: O(1)
Please refer below post for one more approach. 
Print pattern using only one loop


Next Article

Similar Reads