Pattern Printing | Practice | GeeksforGeeks

Given a number N. The task is to print a series of asterisk(*) from 1 till N terms with increasing order and difference being 1.
Example 1:
Input:
N = 3
Output:
* ** ***
Explanation:
First, print 1 asterisk then space after
that print 2 asterisk and space after that 
print 3 asterisk now stop as N is 3.

Example 2:
Input:
N = 5
Output:
* ** *** **** ***** 
Explanation:
First, print 1 asterisk then space after
that print 2 asterisk and space and do this
3 more times.
Your Task:  
You don't need to read input. Your task is to complete the function printPattern() which takes an integer N as an input parameter and print the above pattern.
Expected Time Complexity: O(N^2)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 1000
0
dalalrinku88901 month ago
class Solution{public:   void printPattern(int N)   {      for(int i=1;i<=N;i++)      {          for(int j=1;j<=i;j++)          {          cout<<"*";   }cout<<" ";   }      }}; 
0
amiransarimy3 months ago
Python One Line Solutions
 
class Solution:
   def printPattern(self, N):
       for i in range(1,N+1):
           print("*"*i,end =' ')
0
sonalipatro3 months ago
class Solution{   static void printPattern(int N){       // code here       int i=1;       while(i<=N){           for(int j=0;j<i;j++){               System.out.print("*");           }           System.out.print(" ");i++;       }   }}
0
ankit2030singh3 months ago
**************Python Code*******************
write this in Solution class
def printPattern(self, N):    for i in range(1, N+1):        for j in range(i):            print('*', end = '')        if(i<N):            print(' ', end = '')
0
pankajkumarravi6 months ago
******************* java Logic **************
    static void printPattern(int N){       // code here       for (int i=0;i<N;i++) {           for (int j = 0; j <= i; j++) {               System.out.print("*" );           }           System.out.print(" ");       }   }
0
cshubham4397 months ago
 for(int i = 0; i<N; i++){           for(int j = 0; j<=i; j++){               System.out.print("*");           }           System.out.print(" ");       }
0
Yash Kadulkar1 year ago
Yash Kadulkar
Using for loop:for i in range(1,N+1):        print('*'*i,end=' ')
Using while loop:      L = 1      while L<=N:            print('*'*L,end=' ')            L += 1
0
Shreyansh Kumar Singh1 year ago
Shreyansh Kumar Singh
For Java it's only working when we add a new line at the end of function.Please fix this..!!
0
jay agrawal1 year ago
jay agrawal
class Solution{    static void printPattern(int N){        // code here        for (int i=1;i<=N;i++){            for (int j=1;j<=i;j++){                System.out.print("*");            }            System.out.print(" ");        }    }}My code is not working at N=84,don't know why
0
Vishal Sharma1 year ago
Vishal Sharma
int i,j;        for(i=0; i<n; i++){="" for(j="0;" j<i+1;="" j++){="" cout<<"*";="" }="" cout<<"="" ";="" }="" cout<<"\n";="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.