Explain the output for j==0

please explain the how output is working for if (j==0) ;

hi @mahroosanwar0901_2642b14836027bbf
I can see that u have successfully submitted the code and gained full points… is there still anything??

yes I have not get something in my code ( but it works luckily) , please explain the how output is working for if (j==0) ; in line 21(in my code)

hi @mahroosanwar0901_2642b14836027bbf
so in pattern questions, without u giving ur approach its difficult to figure it out…
U can refer this explanation and code for the same–>
In Pattern Question, there is a common method which we teach in course viz, taking nsp, nst, csp, cst and row variables.

Pattern Hack: Always first Try to first print pattern by ignoring the value to be printed then accommodate your value in that pattern. For e.g.,

     5                   5 
     5 4               4 5 
     5 4 3           3 4 5 
     5 4 3 2       2 3 4 5 
     5 4 3 2 1   1 2 3 4 5 
     5 4 3 2 1 0 1 2 3 4 5 
     5 4 3 2 1   1 2 3 4 5 
     5 4 3 2       2 3 4 5 
     5 4 3           3 4 5 
     5 4               4 5 
     5                   5 

View it as:-

     *                   * 
     * *               * * 
     * * *           * * * 
     * * * *       * * * * 
     * * * * *   * * * * * 
     * * * * * * * * * * * 
     * * * * *   * * * * * 
     * * * *       * * * * 
     * * *           * * * 
     * *               * * 
     *                   *  

Short Info about the variables:-

  1. nsp (number of spaces)-> Number of spaces in very First Line of the pattern.
  2. nst (number of stars)-> Number of stars in very first line of the pattern.
  3. csp (counter of spaces)-> counter of spaces that will print the required number of spaces and will be initialized with 1 and incremented upto nsp.
  4. cst (counter of stars)-> counter of spaces that will print the required number of stars and will be initialized with 1 and incremented upto nst.
  5. rows -> It will be initialized with 1 and will go upto the total number of rows in the pattern.
    In the given pattern,

First build an intuition that u need to work for first row and at the end of the loop need to do work for the next row.
For each row first you need to print your pattern in decreasing order then spaces and then the same pattern but in increasing order.
To build the whole pattern just repeat this work according to total number of rows. With this approach, things seems to work out easily.
Code:

import java.util.Scanner;
public class Main {

   public static void main(String[] args) {

    Scanner scn = new Scanner(System.in);

    int n = scn.nextInt();

    int rows = 2 * n + 1;  // Number of rows

    int nst = 1;                 // initializing number of numbers in a row
    int nsp = 2 * n - 1;     // initializing number of spaces in a row 
    for (int row = 1; row <= rows; row++) {
        int val = n;

         //work for numbers
        for (int cst = 1; cst <= nst; cst++) {
            System.out.print(val + " ");
            val--;
        }

        //work for spaces
            for (int csp = 1; csp <= nsp; csp++) {
            System.out.print("  ");
        }

        int cst = 1;
        if (row == n + 1) {
            cst = 2;
            val += 2;
        } else {
            cst = 1;
            val++;
        }

         //work for numbers
        for (; cst <= nst; cst++) {
            System.out.print(val + " ");
            val++;
        }


        //preparation for next row
        if (row <= n) {
            nsp -= 2;
            nst++;
        } else {
            nsp += 2;
            nst--;
        }
        System.out.println();
    }
}

}

I don’t know java but I get your approach.
but let me tell my approach for j=0 column initialize value j=n and decrease its value as column increase (as j=1,j=2 …)
now I used some comment please check out my saved code

https://ide.codingblocks.com/s/650027

hi @mahroosanwar0901_2642b14836027bbf
so by using this

if(j!=0) 

we avoid printing of 0 twice, bcoz at that vertex, only one 0 is expected… rest ur code imperfectly fine…

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.