I don't understand Pattern 17 solution

I don’t understand the solution you have told me about this question. Is there any other way, please let me know, and please tell me the easy way.You are a variable, you don’t even understand how you will work, which is a problem for me to understand.

@khareswaraj,

What variable / part of the solution is not clear?

how to decide which variable are declare and when?

@khareswaraj,

We declare the number of rows by ‘n’.

Now the lets look at the work being done in every row.

  1. First we print some stars.
  2. Then in the same row, we print a blank space
  3. Then we have to again print some stars.

Now if you look closely, work being done in 1. and 3. is same. That means we have to print the same number of stars.

Now for 1 and 3 we keep a variable nst (number of stars) and for 2 we keep a variable nsp (number of spaces).

Now we initialize nst as n/2 and nsp as 1.

Preparation for next row:

Now if you look closely from 4th row onwards we need to decrement our spaces and increment the number of stars.

if(present_row<=n/2){
      nsp = nsp + 2; //till n/2 spaces increase by 2.
      nst = nst - 1; //till n/2 stars decrease by 1.
}
else{
      nsp = nsp - 2; //after n/2 spaces decrease by 2.
      nst = nst + 1; //after n/2 stars increase by 1.

}

This is code and use of variables. Just using 3 variables. n which is number of rows. nst which is number of stars. nsp which is number of spaces.
Also, apologizes for the late reply.

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.