Function of this code as of why all new lines are executed at first then output stars is displayed

public class Pattern {
static void pattern(int row,int col,int n)
{
if(n==row)
return;
if(col>row) {
System.out.println();
pattern(row+1,1,n);

		return ;
	}
	
	
	pattern(row,col+1,n);
	System.out.print("* ");
	
}
public static void main(String args[])
{
	int r=1,c=1;
	pattern(r,c,5);
}

}

this is because your stmt System.out.print("* “); is written after recursive call, while new line insertion stmt is before recursive call.
just change the order
like System.out.print(”* "); pattern(row,col+1,n); and you will get your answer.

Thank you,.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.