Home > Enterprise >  Print this Alphabetical Pattern using 2 loops
Print this Alphabetical Pattern using 2 loops

Time:05-09

I need to print this pattern in java using only 2 loops one for the row and the second for the column.

A B C D E 
 B C D E 
  C D E 
   D E 
    E  
   D E 
  C D E 
 B C D E 
A B C D E

What I've tried

public class Main{
    public static void main(String[] args) {
        int n = 5;
        char c = 'A';
        for(int i=1; i<=n; i  ){
            int k=1;
            for(int j=1; j<=(n*2)-1; j  ){
                if(j>=i && j<=((n*2)-i) && k!=0){
                    System.out.print(c);
                    k=0;
                } else {
                    System.out.print(" ");
                    k=1;
                    c  ;
                }
            }
            c = 'A';
            System.out.println();
        }   
    }
}

Output: This logic is printing half of the pattern correctly as I'm unable to understand how to built for the second half.

CodePudding user response:

Try using the same logic as you used to print the top half, with the outer for-loop reversed, i.e., change:

for (int i = 1; i <= n; i  ) {

to:

for (int i = n - 1; i >= 1; i--) {

Full code:

class Main {
  public static void main(String[] args) {
    printHourGlass(5);
  }

  public static void printHourGlass(int n) {
    char c = 'A';
    for (int i = 1; i <= n; i  ) {
      int k = 1;
      for (int j = 1; j <= (n * 2) - 1; j  ) {
        if (j >= i && j <= ((n * 2) - i) && k != 0) {
          System.out.print(c);
          k = 0;
        } else {
          System.out.print(" ");
          k = 1;
          c  ;
        }
      }
      c = 'A';
      System.out.println();
    }
    for (int i = n - 1; i >= 1; i--) {
      int k = 1;
      for (int j = 1; j <= (n * 2) - 1; j  ) {
        if (j >= i && j <= ((n * 2) - i) && k != 0) {
          System.out.print(c);
          k = 0;
        } else {
          System.out.print(" ");
          k = 1;
          c  ;
        }
      }
      c = 'A';
      System.out.println();
    }
  }
}

Output:

A B C D E
 B C D E 
  C D E  
   D E   
    E    
   D E   
  C D E  
 B C D E 
A B C D E

CodePudding user response:

To make this work for any terminating letter from 'B' to 'Z', define some values.

String indent = "                                     ";
char endLetter = 'E';
int end = endLetter - 'A';

The first part is easy and fairly straight forward.

  • use substring with indent to control indenting.
  • then print the characters incrementing the inner and outer loop until done.
for (int k = 0; k <= end; k  ) {
    System.out.print(indent.substring(0,k));
    for (char c = (char)('A'   k); c <= endLetter; c  ) {
        System.out.print(c   " ");
    }
    System.out.println();
}

prints

A B C D E 
 B C D E 
  C D E 
   D E 
    E 

The trick is to reverse the process, continuing the outer loop.

  • the outer loop now goes from 0 to end*2 1
  • and k is replaced by s
    • using int s = k <= end ? k : end*2 - k;
    • s becomes 0 1 2 3 4 ... n ... n-1 n-2 ... 4 3 2 1 0
for (int k = 0; k < end*2 1; k  ) {
    int s = k <= end ? k : end*2 - k;
    System.out.print(indent.substring(0,s));
    for (char c = (char) ('A'   s); c <= endLetter; c  ) {
        System.out.print(c   " ");
    }
    System.out.println();
}

prints

A B C D E 
 B C D E 
  C D E 
   D E 
    E 
   D E 
  C D E 
 B C D E 
A B C D E 

Note that in Java 11 String.repeat was added so that could also be used to indent the appropriate lines. So the printing of the indent could be System.out.print(" ".repeat(s));

  • Related