DEV Community

Cover image for Java Pattern Problem
Readul Islam Niyon
Readul Islam Niyon

Posted on

Java Pattern Problem

Java solution to 24 pattern problems

simple logic behind pattern problems

  1. The number of rows in the pattern are executed by the outer loop.

  2. Inner loop executes over number of columns (calculate column number)

  3. The elements need to print in each column

  4. Careful about new lines and spaces

Pattern 1

 * * * * * * * * * * * * * * * 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 <=n; row++){ //inner loop iterates over row; for(int col = 1; col <=row; col++){ //print *; System.out.print("* "); } //print new line; System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 2

 * * * * * * * * * * * * * * * 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 <= n; row++) { // inner loop iterates over the formula (n + 1)-row ; for(int col = 1; col <= n+1-row; col++) { //print * System.out.print("* "); } // add new line System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 3

 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 <=n; row++){ //inner loop iterates over row; for(int col = 1; col <=row; col++){ //print *; System.out.print(col + " "); } //print new line; System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 4

 * * * * * * * * * * * * * * * * * * * * * * * * * 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 < 2*n; row++) { // calculate number of columns in a row int noOfCol = row > n ? 2*n - row : row; // inner loop iterates over number of columns for(int col = 1; col <= noOfCol; col++) { // print * System.out.print("* "); } // add new line after every row System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 5

 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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 < 2*n; row++){ // calculate number of columns in a row int noOfCols = row > n ? row+1-n : n+1-row; // inner loop iterates over number of columns for(int col = 1; col <= noOfCols; col++) { // print * System.out.print("* "); } // After printing each row, add a new line System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 6

 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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 <= 2*n; row++) { // calculate number of columns in a row int noOfCol = row > n ? 2*n - row+1 : row; // inner loop iterates over number of columns for(int col = 1; col <= noOfCol; col++) { // print * System.out.print("* "); } // add new line after every row System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 7

 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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 <=2*n; row++) { // calculate the number of columns in each row int noOfCols = row > n? row - n : (n+1)-row ; // inner loop iterates over number of columns for(int col = 1; col <= noOfCols; col++) { // print * System.out.print("* "); } // after printing each row, add a new line System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 8

 * * * * * * * * * * * * * * * 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 <= n; row ++) { // number of columns for printing space int noOfSpaces = n-row; // inner loop iterates over Number of spaces for(int space = 1; space <= 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 <= noOfStars; star ++) { System.out.print("* "); } // after printing each row, add a new line System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 9

 * * * * * * * * * * * * * * * 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 <= n; row ++) { // number of columns for printing space int noOfSpaces = row-1; // inner loop iterates over Number of spaces for(int space = 1; space <= 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 <= noOfStars; star ++) { System.out.print("* "); } // after printing each row, add a new line System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 10

 * * * * * * * * * * * * * * * * * * * * * * * * * 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 <=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 <=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 <= noOfStars; star++) { System.out.print("* "); } // after printing each row,add a new line System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 11

 * * * * * * * * * * * * * * * * * * * * * * * * * 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 <=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 <=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 <= noOfStars; star++) { System.out.print("* "); } // after printing each row,add a new line System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 12

 * * * * * * * * * * * * * * * 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 <= n; row ++) { // number of columns for printing space int noOfSpaces = n-row; // inner loop iterates over Number of spaces for(int space = 1; space <= 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 <= noOfStars; star ++) { System.out.print("* "); } // after printing each row, add a new line System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 13

 * * * * * * * * * * * * * * * 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 <= n; row ++) { // number of columns for printing space int noOfSpaces = row-1; // inner loop iterates over Number of spaces for(int space = 1; space <= 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 <= noOfStars; star ++) { System.out.print("* "); } // after printing each row, add a new line System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 14

 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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 <= N; row++) { // number of spaces in the column int noOfSpaces = row > n ? N - row : row - 1; // inner loop iterates over the number of spaces for(int space = 1; space <= noOfSpaces; space++) { System.out.print(" "); } // number of stars in the column int noOfStars = row > n ? row-n : n-row+1; // inner loop iterates over the number of stars for(int stars = 1; stars <= noOfStars; stars++) { System.out.print("* "); } // add a new line after printing rows System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 15

 * * * * * * * ********* 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 <=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 <=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 <= 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(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 16

 ********* * * * * * * * 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 <=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 <= 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 <= 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(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 17

 ***** * * * * * * ***** 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 <=l; row ++) { // number of stars and spaces in a row; int noOfStars = l ; // inner loop executes over noOfStars; for(int star = 1; star <=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(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 18

 * * * * * * * * * * * * * * * * * * * * * * * * * 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 <= l; row++) { // number of Spaces before printing stars int noOfSpaces = row - 1; // inner loop executes over noOFSpaces; for(int space = 1; space <= noOfSpaces; space++) { // print space System.out.print(" "); } // number of stars; int noOfStars = l; // inner loop executes over noOfStars; for(int star = 1; star <= noOfStars; star++) { // print star System.out.print("* "); } // add a new line before executes next row System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 19

 ***** * * * * * * ***** 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 <=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 <= 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 <=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(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 20

 ***** * * * * * * ***** 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 <=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 <= 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 <=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(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 21

 * * * * * * * * * * * * * * * * 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 <= l; row++) { // number of spaces before printing each column int noOfSpaces = row > n ? row - n : n - row ; // inner loop iterates over noOfSpaces; for(int space = 1; space <= noOfSpaces; space++) { // print spaces System.out.print(" "); } // number of stars in a column int noOfStars = row > n ? 2*n - row : row ; // inner loop iterates over noOfStars; for(int star = 1; star <= 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(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 22

 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 <= 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 <= noOfSpaces; space++) { // print spaces System.out.print(" "); } // inner loop iterates over row; for(int col = 1 ; col <= 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(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 23

 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 <= N; row++) { // number of spaces in a row int noOfSpaces = row > n ? row - n : n - row ; // inner loop iterates over noOfSpaces; for(int space = 1; space <= noOfSpaces; space++) { // print space System.out.print(" "); } // number of cols c; int c = row > n ? 2*n-row : row ; // from c to 1 for(int col = c; col >= 1 ; col--) { System.out.print(col); } // from 2 to c; for(int col = 2; col <=c ; col++) { System.out.print(col); } System.out.println(); } } 
Enter fullscreen mode Exit fullscreen mode

Pattern 24

 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 <= n; row++) { // inner loop executes each col till number of rows; for (int col = 1; col <= 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(); } } 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)