Doubt in a Pattern Problem

So here is my code:

Now, I am not understanding the code from line 19 to 27 (i.e. the code inside else block}. I am totally confused in the condition used in while loop( line 22).

Hey @yashsharma4304 in line number 22 what you are doing is you got an even row now let it be 6 , so first you are printing 1 now you have to print 4 zeroes and then again 1 so that total numbers in this line will be 6. That’s the reason we have done col<=row-2 because the two spots are booked for 1 and rest are for zeroes. Hope this would help you :slight_smile:

So, that means there are two 1’s that has already booked first and the last column. Now, does it mean that (the column number 2 is column number 1 for that while loop and column number 5 is column number 4) for that while loop.

Row = 6
Row - 2 = 4
that means Col <= Row - 2
i.e. Col <= 4

and therefore I am asking that the loop will run 4 times. So, actually column 2 is column 1 and column 5 is column 4 for that while loop. Right?

Wait you are getting confused,
Suppose your row is 6 okay,
And in your 6th row you are at column number 1
Here you have to print 1 , now there are 5 more columns left because your row is 6
Now what you have to do is in your 5 more columns you have to add 4 zeroes
Resulting 10000 now you have only one spot left that is your 6th column, here you have to add 1 which gives you a resultant of 1000001 . Hope this would help you in understanding it :slight_smile:

1 Like

Yes, now I understand. Thank you for resolving my doubt. Great Explanation.

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.