Print the given pattern questions

In this article, we have explored how to print a given pattern. We have explored over 15 given patterns.

1. Given number of rows and columns, write a program to print a solid rectangle using stars.

Example: Number of rows: 3, Number of columns: 5

* * * * *
* * * * *
* * * * *
    void solidRectangle(int n, int m) {
        int i, j;
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

2. Given a number n, print pascal's triangle till n rows

Example: n = 4

       1
     1   1
   1   2   1
 1   3   3   1
    public void pascalsTriangle(int n) {
        for (int row = 1; row <= n; row++) {
            for (int i = 0; i <= n - row; i++) {
                System.out.print(" ");
            }
            int num = 1;
            for (int i = 1; i <= row; i++) {
                System.out.print(num + " ");
                num = num * (row - i) / i;
            }
            System.out.println();
        }
    }

3. Given a number n, print a butterfly pattern.

Example: n = 4

*              *
* *          * *
* * *      * * *
* * * *  * * * *
* * * *  * * * *
* * *      * * *
* *          * *
*              *
    public void butterflyPattern(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print("* ");
            }
            for (int j = 0; j < (2 * n) - (2 * i); j++) {
                System.out.print("  ");
            }
            for (int j = 0; j < i; j++) {
                System.out.print(" *");
            }
            System.out.println();
        }

        for (int i = n; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                System.out.print("* ");
            }
            for (int j = 0; j < (2 * n) - (2 * i); j++) {
                System.out.print("  ");
            }
            for (int j = 0; j < i; j++) {
                System.out.print(" *");
            }
            System.out.println();
        }
    }

4. Write a program to print the diamond pattern as shown in the example given below:

Example: n = 5

    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1
    public void diamondPattern(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            for (int k = i; k >= 1; k--) {
                System.out.print(k);
            }
            for (int l = 2; l <= i; l++) {
                System.out.print(l);
            }
            System.out.println();
        }
        for (int i = n - 1; i >= 1; i--) {
            for (int j = 0; j < n - i; j++) {
                System.out.print(" ");
            }
            for (int k = i; k >= 1; k--) {
                System.out.print(k);
            }
            for (int l = 2; l <= i; l++) {
                System.out.print(l);
            }
            System.out.println();
        }
    }

5. Given a number n, print Floyd's triangle pattern till n rows.

Example: n = 5

1  
2  3  
4  5  6  
7  8  9  10         
11  12  13  14  15 
    public static void floydsTriangle(int n) {
        int num = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(num + "  ");
                num++;
            }
            System.out.println();
        }
    }

6. Write a program to print a pyramid using stars till n rows.

Example: n = 5

    *
   * *
  * * *
 * * * *
* * * * *
    public void pyramid(int n) {
        for (int i = 0; i < n; i++) {
            for (int j = n - i; j > 1; j--) {
                System.out.print(" ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }

7. Using stars, print a sandglass pattern till n rows.

Example: n = 5

* * * * * 
 * * * *  
  * * *   
   * *    
    *     
    *     
   * *    
  * * *   
 * * * *  
* * * * * 
    public void sandglassPattern(int n) {
        for (int i = 0; i <= n - 1; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print(" ");
            }
            for (int k = i; k <= n - 1; k++) {
                System.out.print("*" + " ");
            }
            System.out.println("");
        }
        for (int i = n - 1; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                System.out.print(" ");
            }
            for (int k = i; k <= n - 1; k++) {
                System.out.print("*" + " ");
            }
            System.out.println("");
        }
    }

8. Given a string, print every character of the string in the increasing order of odd numbers as depicted

Example: s = "HELLO"

    H    
   EEE   
  LLLLL  
 LLLLLLL 
OOOOOOOOO
    public void printString(String s) {
        for (int i = 1; i <= s.length(); i++) {
            for (int j = s.length(); j > i; j--) {
                System.out.print(" ");
            }
            for (int k = (i * 2) - 1; k >= 1; k--) {
                System.out.print(s.charAt(i - 1));
            }
            System.out.println("");
        }
    }

9. Write a program to print a hollow triangle pattern till n rows.

Example: n = 5

    *    
   * *   
  *   *  
 *     * 
*********
    public void hollowTriangle(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = i; j < n; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= (2 * i - 1); k++) {
                if (k == 1 || i == n || k == (2 * i - 1)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println("");
        }
    }

10. Predict the output of the following code

public static void main(String args[]) {
        int n = 8;
        for (int i = 0; i <= n - 1; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k <= n - 1 - i; k++) {
                System.out.print("*" + " ");
            }
            System.out.println();
        }
    }

The code will produce an inverted pyramid till n rows. The first nested loop prints space till i columns and the next nested loop prints stars. The number of stars decreases progressively with each row.

Output:

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

11. Predict the output of the following code

    public static void main(String args[]) {
        int n = 5;
        for (int i = 0; i < n; i++) {
            for (int j = n - i - 1; j > 0; j--) {
                System.out.print("  ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }

The code generates a left star triangle pattern till 5 rows. The first inner loop prints space which decrements by one. The next inner loop prints star. The number of stars in each row is the same as row number(5 in this case).

Output:

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

12. Predict output of the following code:

public static void main(String args[]) {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + " ");
            }

            System.out.println();
        }
    }

The code print n number of rows (5 in this case). The inner loop prints i (the row number) i times. Each row contains columns equal to the row number. Each column contains the row number.

Output:

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

13. What should be changed in the following program to print the desired output:

    public static void main(String args[]) {
        int n = 5;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= n; j++) {
                System.out.print("* ");
            }
        }
    }

Output:

* 
* *       
* * *     
* * * *   
* * * * * 
  1. The inner loop should run i times only since the columns in each row is equal to the row number.
  2. After the inner loop, a print statement should be added so that the cursor moves to the next line.

The correct program:

public static void main(String args[]) {
        int n = 5;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }

14. What should be changed in the following program to print the desired output:

    public static void main(String args[]) {
        int n = 5;
        for (int i = n; i > 0; i--) {
            int num = 1;
            for (int j = 0; j <= i; j++) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }

Output:

1 
1 2       
1 2 3     
1 2 3 4   
1 2 3 4 5 
  1. Either the iterator in the first loop should start from 0 till n or the inner loop should run till n - 1.
  2. The variable num has to be incremented every time the inner loop is executed.

The correct code:

    public static void main(String args[]) {
        int n = 5;
        for (int i = 0; i < n; i++) {
            int num = 1;
            for (int j = 0; j <= i; j++) {
                System.out.print(num + " ");
                num++;
            }
            System.out.println();
        }
    }

15. Complete the code to print the letter A in stars

    public static void main(String args[]) {
        int n = 8;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < n / 2; j++) {
                System.out.print("* ");
            }
            _____________
            for (int j = 0; j < n / 2 - 1; _______) {
                System.out.print("* ");
                for (int k = 0; k < (n / 2) - 2; k++) {
                    System.out.print(______);
                }
                System.out.println("* ");
            }
        }
    }

Output:

* * * * 
*     * 
*     * 
*     * 
* * * * 
*     * 
*     * 
*     * 
  1. System.out.println() This statement will move the cursor to the next line after printing the n/2 stars in one line.
  2. j++ This is added to increment the iterator.
  3. " " Space is printed so that the stars could be printed at their respective locations.

The correct code:

    public static void main(String args[]) {
        int n = 8;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < n / 2; j++) {
                System.out.print("* ");
            }
            System.out.println();
            for (int j = 0; j < n / 2 - 1; j++) {
                System.out.print("* ");
                for (int k = 0; k < (n / 2) - 2; k++) {
                    System.out.print("  ");
                }
                System.out.println("* ");
            }
        }
    }