<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Readul Islam Niyon</title>
    <description>The latest articles on DEV Community by Readul Islam Niyon (@niyon01).</description>
    <link>https://dev.to/niyon01</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F881883%2Fc9ff74ed-a3a4-4f8b-8249-1ce2ad1e6a9a.png</url>
      <title>DEV Community: Readul Islam Niyon</title>
      <link>https://dev.to/niyon01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/niyon01"/>
    <language>en</language>
    <item>
      <title>Java Pattern Problem</title>
      <dc:creator>Readul Islam Niyon</dc:creator>
      <pubDate>Tue, 11 Oct 2022 14:45:31 +0000</pubDate>
      <link>https://dev.to/niyon01/java-pattern-problem-3dcl</link>
      <guid>https://dev.to/niyon01/java-pattern-problem-3dcl</guid>
      <description>&lt;h2&gt;
  
  
  Java solution to 24 pattern problems
&lt;/h2&gt;

&lt;p&gt;simple logic behind pattern problems&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The number of rows in the pattern are executed by the outer loop.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inner loop executes over number of columns (&lt;em&gt;calculate column number&lt;/em&gt;)&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The elements need to print in each column&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Careful about new lines and spaces&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Pattern 1
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
     *
     * *
     * * *
     * * * *
     * * * * *



   public static void main(String[] args) {

        // number of lines in the pattern
        int n = 5;
        //outer loop iterates over n;
        for(int row = 1; row &amp;lt;=n; row++){
            //inner loop iterates over row;
            for(int col = 1; col &amp;lt;=row; col++){
                //print *;
                System.out.print("* ");
            }
            //print new line;
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
     * * * * *
     * * * *
     * * *
     * *
     *



 public static void main(String[] args) {
        // number of lines in the pattern;
        int n = 5;
        // outer loop iterates over number of lines;
        for(int row = 1; row &amp;lt;= n; row++) {
            // inner loop iterates over the formula (n + 1)-row ;
            for(int col = 1; col &amp;lt;= n+1-row; col++) {
                //print *
                System.out.print("* ");
            }
            // add new line
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 3
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


 public static void main(String[] args) {
        // number of lines in the pattern
        int n = 5;
        //outer loop iterates over n;
        for(int row = 1; row &amp;lt;=n; row++){
            //inner loop iterates over row;
            for(int col = 1; col &amp;lt;=row; col++){
                //print *;
                System.out.print(col + " ");
            }
            //print new line;
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 4
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


public static void main(String[] args) {
        // number of lines look similar
        int n = 5;
        // outer loop iterates over number of lines in the pattern
        for(int row = 1; row &amp;lt; 2*n; row++) {
            // calculate number of columns in a row
            int noOfCol = row &amp;gt; n ? 2*n - row : row;
            // inner loop iterates over number of columns
            for(int col = 1; col &amp;lt;= noOfCol; col++) {
                // print *
                System.out.print("* ");
            }
            // add new line after every row
            System.out.println();
        }

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 5
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


  public static void main(String[] args) {
        // number of lines look similar
        int n = 5;
        // outer loop iterates over number of lines in the pattern
        for(int row = 1; row &amp;lt; 2*n; row++){
            // calculate number of columns in a row
            int noOfCols = row &amp;gt; n ? row+1-n : n+1-row;
            // inner loop iterates over number of columns
            for(int col = 1; col &amp;lt;= noOfCols; col++) {
                // print *
                System.out.print("* ");
            }
            // After printing each row, add a new line
            System.out.println();
        }

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 6
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


  public static void main(String[] args) {
        // number of lines look similar
        int n = 5;
        // outer loop iterates over number of lines in the pattern
        for(int row = 1; row &amp;lt;= 2*n; row++) {
            // calculate number of columns in a row
            int noOfCol = row &amp;gt; n ? 2*n - row+1 : row;
            // inner loop iterates over number of columns
            for(int col = 1; col &amp;lt;= noOfCol; col++) {
                // print *
                System.out.print("* ");
            }
            // add new line after every row
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 7
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


    public static void main(String[] args) {
        // number of similar lines that don't repeated
        int n = 5;
        // outer loop iterates over number of lines
        for(int row = 1; row &amp;lt;=2*n; row++) {
            // calculate the number of columns in each row
            int noOfCols = row &amp;gt; n? row - n : (n+1)-row ;
            // inner loop iterates over number of columns
            for(int col = 1; col &amp;lt;= noOfCols; col++) {
                // print *
                System.out.print("* ");
            }
            // after printing each row, add a new line
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 8
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


    public static void main(String[] args) {
        // number of lines in the pattern
        int n = 5;
        // outer loop iterates over Number of lines which is rows
        for(int row = 1; row &amp;lt;= n; row ++) {
            // number of columns for printing space
            int noOfSpaces = n-row;
            // inner loop iterates over Number of spaces
            for(int space = 1; space &amp;lt;= noOfSpaces; space ++) {
                System.out.print("  ");
            }
            // number of columns for printing star
            int noOfStars = row;
            // inner loop iterates over Number of stars
            for(int star = 1; star &amp;lt;= noOfStars; star ++) {
                System.out.print("* ");
            }
            // after printing each row, add a new line
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 9
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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



   public static void main(String[] args) {


        // number of lines in the pattern
        int n = 5;

        // outer loop iterates over Number of lines which is rows
        for(int row = 1; row &amp;lt;= n; row ++) {
            // number of columns for printing space
            int noOfSpaces = row-1;

            // inner loop iterates over Number of spaces
            for(int space = 1; space &amp;lt;= noOfSpaces; space ++) {
                System.out.print("  ");
            }

            // number of columns for printing star
            int noOfStars = n+1-row;

            // inner loop iterates over Number of stars
            for(int star = 1; star &amp;lt;= noOfStars; star ++) {
                System.out.print("* ");
            }
            // after printing each row, add a new line
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 10
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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



    public static void main(String[] args) {

        // number of lines in the pattern
        int n = 5;
        // outer loop executes until the number of lines is reached
        for(int row = 1; row &amp;lt;=n; row++) {
            // number of spaces in the columns
            int noOfSpaces = n - row;
            // inner loop executes until the number of spaces  is reached
            for(int space = 1; space &amp;lt;=noOfSpaces; space++) {
                System.out.print("  ");
            }
            // number of stars in the columns
            int noOfStars = row + row-1;
            // inner loop executes until the number of stars is reached
            for(int star = 1; star &amp;lt;= noOfStars; star++) {
                System.out.print("* ");
            }
            // after printing each row,add a new line
            System.out.println();
        }

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 11
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


    public static void main(String[] args) {

        // number of lines in the pattern
        int n = 5;
        // outer loop executes until the number of lines is reached
        for(int row = 1; row &amp;lt;=n; row++) {
            // number of spaces in the columns
            int noOfSpaces = row-1;
            // inner loop executes until the number of spaces  is reached
            for(int space = 1; space &amp;lt;=noOfSpaces; space++) {
                System.out.print("  ");
            }
            // number of stars in the columns
            int noOfStars = 2* (n-row)+1;
            // inner loop executes until the number of stars is reached
            for(int star = 1; star &amp;lt;= noOfStars; star++) {
                System.out.print("* ");
            }
            // after printing each row,add a new line
            System.out.println();
        }

    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 12
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


    public static void main(String[] args) {

        // number of lines in the pattern
        int n = 5;
        // outer loop iterates over Number of lines which is rows
        for(int row = 1; row &amp;lt;= n; row ++) {
            // number of columns for printing space
            int noOfSpaces = n-row;
            // inner loop iterates over Number of spaces
            for(int space = 1; space &amp;lt;= noOfSpaces; space ++) {
                System.out.print(" ");
            }
            // number of columns for printing star
            int noOfStars = row;
            // inner loop iterates over Number of stars
            for(int star = 1; star &amp;lt;= noOfStars; star ++) {
                System.out.print("* ");
            }
            // after printing each row, add a new line
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 13
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


   public static void main(String[] args) {

        // number of lines in the pattern
        int n = 5;
        // outer loop iterates over Number of lines which is rows
        for(int row = 1; row &amp;lt;= n; row ++) {
            // number of columns for printing space
            int noOfSpaces = row-1;
            // inner loop iterates over Number of spaces
            for(int space = 1; space &amp;lt;= noOfSpaces; space ++) {
                System.out.print(" ");
            }
            // number of columns for printing star
            int noOfStars = n-row+1;
            // inner loop iterates over Number of stars
            for(int star = 1; star &amp;lt;= noOfStars; star ++) {
                System.out.print("* ");
            }
            // after printing each row, add a new line
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 14
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


    public static void main(String[] args) {
        // number of lines in the pattern which is rows
        int N = 10;
        // number of similar lines in the pattern
        int n = 5;
        // outer loop iterates over the number of lines N
        for (int row = 1; row &amp;lt;= N; row++) {
            // number of spaces in the column
            int noOfSpaces = row &amp;gt; n ? N - row : row - 1;
            // inner loop iterates over the number of spaces
            for(int space = 1; space &amp;lt;= noOfSpaces; space++) {
                System.out.print(" ");
            }
            // number of stars in the column
            int noOfStars = row &amp;gt; n ? row-n : n-row+1;
            // inner loop iterates over the number of stars
            for(int stars = 1; stars &amp;lt;= noOfStars; stars++) {
                System.out.print("* ");
            }
            // add a new line after printing rows
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 15
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


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


   public static void main(String[] args) {

        // number of lines in the pattern
        int n = 5;
        // outer loop will be executed till the number of lines
        for(int row = 1; row &amp;lt;=n; row++) {
            // number of spaces in the column before printing star
            int noOfSpaces = n -row;
            //inner loop executes until the noOfSpaces is reached
            for(int space = 1; space &amp;lt;=noOfSpaces; space++) {
                // print spaces
                System.out.print(" ");
            }
            // number of stars and spaces in the pattern
            int noOfStars = row + row -1;
            // inner loop executes until the noOfStars is reached
            for(int star = 1; star &amp;lt;= noOfStars; star++){
                // print stars in first, last in the column and last row
                if(star ==1 || star == noOfStars || row == n){
                    System.out.print("*");
                }
                // otherwise print spaces
                else System.out.print(" ");

            }
            // adding a new line after printing each row
            System.out.println();
        }

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 16
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


    public static void main(String[] args) {

        // number of lines in the pattern
        int n = 5;
        // outer loop will be executed till the number of lines
        for(int row = 1; row &amp;lt;=n; row++) {
            // number of spaces in the column before printing star
            int noOfSpaces = row-1;
            //inner loop executes until the noOfSpaces is reached
            for (int space = 1; space &amp;lt;= noOfSpaces; space++) {
                // print spaces
                System.out.print(" ");
            }
            // number of stars and spaces in the pattern
            int noOfStars = 2*n-row- noOfSpaces;
            // inner loop executes until the noOfStars is reached
            for (int star = 1; star &amp;lt;= noOfStars; star++) {
                // print stars in first, last in the column and last row
                if (star == 1 || star == noOfStars || row == 1) {
                    System.out.print("*");
                }
                // otherwise print spaces
                else System.out.print(" ");

            }
            // adding a new line after printing each row
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 17
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


   public static void main(String[] args) {

        // number of lines in the pattern
        int l = 5;
        // outer loop executes over l
        for(int row = 1; row &amp;lt;=l; row ++) {
            // number of stars and spaces in a row;
            int noOfStars = l ;
            // inner loop executes over noOfStars;
            for(int star = 1; star &amp;lt;=noOfStars; star ++) {
                // print star in column first,column last, row first and row last positions
                if(star == 1 || star == noOfStars || row == l || row == 1){
                    System.out.print("*");
                }
                // print spaces
                else System.out.print(" ");
            }
            // add a new line after each row
            System.out.println();
        }

    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 18
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


    public static void main(String[] args) {

        // total lines in the pattern
        int l = 5;

        // outer loop executes over the l
        for(int row = 1; row &amp;lt;= l; row++) {

            // number of Spaces before printing stars
            int noOfSpaces = row - 1;
            // inner loop executes over noOFSpaces;
            for(int space = 1; space &amp;lt;= noOfSpaces; space++) {
                // print space
                System.out.print(" ");
            }
            // number of stars;
            int noOfStars = l;
            // inner loop executes over noOfStars;
            for(int star = 1; star &amp;lt;= noOfStars; star++) {
                // print star
                System.out.print("* ");
            }
            // add a new line before executes next row
            System.out.println();
        }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 19
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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


    public static void main(String[] args) {

        // number of lines in the pattern
        int l = 5;
        // outer loop for executing the number of rows which is equal to number of lines l
        for(int row = 1; row &amp;lt;=l; row ++) {

            // number of spaces in the column before printing stars
            int noOfSpaces = l - row;
            // inner loop executes over noOfSpaces
            for(int spaces = 1; spaces &amp;lt;= noOfSpaces; spaces++) {
                // print stars before printing stars
                System.out.print(" ");
            }
            // number of stars and middle spaces
            int noOfStars = l;
            // inner loop executes over noOfStars
            for(int stars = 1; stars &amp;lt;=noOfStars; stars++) {
                // print stars in  first and last position of column and row
                if(stars == 1 || stars == noOfStars || row == 1 || row == l){
                    System.out.print("*");
                }
                // otherwise print middle spaces
                else System.out.print(" ");
            }
            // add a new line after each row
            System.out.println();
        }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 20
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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



   public static void main(String[] args) {

        // number of lines in the pattern
        int l = 5;
        // outer loop for executing the number of rows which is equal to number of lines l
        for(int row = 1; row &amp;lt;=l; row ++) {

            // number of spaces in the column before printing stars
            int noOfSpaces = row -1;
            // inner loop executes over noOfSpaces
            for(int spaces = 1; spaces &amp;lt;= noOfSpaces; spaces++) {
                // print stars before printing stars
                System.out.print(" ");
            }
            // number of stars and middle spaces
            int noOfStars = l;
            // inner loop executes over noOfStars
            for(int stars = 1; stars &amp;lt;=noOfStars; stars++) {
                // print stars in  first and last position of column and row
                if(stars == 1 || stars == noOfStars || row == 1 || row == l){
                    System.out.print("*");
                }
                // otherwise print middle spaces
                else System.out.print(" ");
            }
            // add a new line after each row
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 21
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


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



    public static void main(String[] args) {
        // number of lines
        int l = 9;
        // number of similar lines
        int n = l/2+1;
        // outer loop iterates over l;
        for(int row = 1; row &amp;lt;= l; row++) {
            // number of spaces before printing each column
            int noOfSpaces = row &amp;gt; n ? row - n : n - row ;
            // inner loop iterates over noOfSpaces;
            for(int space = 1; space &amp;lt;= noOfSpaces; space++) {
                // print spaces
                System.out.print(" ");
            }
            // number of stars in a column
            int noOfStars = row &amp;gt; n ?  2*n - row : row ;
            // inner loop iterates over noOfStars;
            for(int star = 1; star &amp;lt;= noOfStars; star++) {
                // star print in first and last column in a row
                if(star == 1 || star == noOfStars) {
                    System.out.print("* ");
                }
                // otherwise print spaces
                else System.out.print("  ");
            }
            // add a new line after each row
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 22
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

        1
       1 1
      1 2 1
     1 3 3 1
    1 4 6 4 1



    public static void main(String[] args) {

        // number of lines
        int n = 5;
        // outer loop iterates over n;
        for(int row = 1; row &amp;lt;= n; row++) {
            // set num = 1;
            int num = 1;
            // number of spaces before printing elements
            int noOfSpaces = n - row ;
            // outer loop iterates over noOfSpaces;
            for(int space = 1; space &amp;lt;= noOfSpaces; space++) {
                // print spaces
                System.out.print(" ");
            }
            // inner loop iterates over row;
            for(int col = 1 ; col &amp;lt;= row; col++) {
                // print num;
                System.out.print(num + " ");
                // update num value for next number
                num = num * (row - col)/ (col);
            }
            // add a new line
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 23
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


   1
  212
 32123
4321234
 32123
  212
   1



    public static void main(String[] args) {

        // number of input
        int n = 4;
        // number of lines
        int N = 2*n-1;
        // outer loop iterates over N;
        for(int row = 1; row &amp;lt;= N; row++) {
            // number of spaces in a row
            int noOfSpaces = row &amp;gt; n ? row - n : n - row ;
            // inner loop iterates over noOfSpaces;
            for(int space = 1; space &amp;lt;= noOfSpaces; space++) {
                // print space
                System.out.print(" ");
            }
            // number of cols c;
            int c = row &amp;gt; n ? 2*n-row : row ;
            // from c to 1
            for(int col = c; col &amp;gt;= 1 ; col--) {
                System.out.print(col);
            }
            // from 2 to c;
            for(int col = 2; col &amp;lt;=c ; col++) {
                System.out.print(col);
            }
            System.out.println();
        }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pattern 24
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


1
0 1
1 0 1
0 1 0 1
1 0 1 0 1



    public static void main(String[] args) {
        // number of lines
        int n = 5;
        // outer loop iterates over n;
        for (int row = 1; row &amp;lt;= n; row++) {

            // inner loop executes each col till number of rows;
            for (int col = 1; col &amp;lt;= row; col++) {
                // print 1 for even
                if ((row + col) % 2 == 0) {
                    System.out.print(1+ " ");
                }
                // print 0 for odd
                else {
                    System.out.print(0+ " ");
                }
            }
            // add a new line
            System.out.println();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>pattern</category>
      <category>problem</category>
      <category>logic</category>
      <category>java</category>
    </item>
  </channel>
</rss>
