why is nl2 being used for initialisation why cant we only use n=3 and followed while we have to decrement it will be 3-1 something , also i expect reply from garima ma’am please
Why have we used n/2 for the initialisation
We declare the number of rows by ‘n’.
n/2 approach will always work when the upper and lower part of diagram is symmetrical.
so, to draw the lower part just reverse the approach of drawing upper part .
Now the lets look at the work being done in every row.
- First we print some stars.
- Then in the same row, we print a blank space
- 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.