Java Program to Print Windows Character Pattern

Program to Print Windows Character Pattern

In the previous article, we have discussed Java Program to Print Crown Character Pattern

In this article we are going to see how to print window character pattern.

Example-1:

When number of rows : 9 
Odd number of rows then only one middle element(mid row + mid column).

A B C D E F G H I 
A           E           I 
A           E           I 
A           E           I 
A B C D E F G H I 
A           E           I 
A           E           I 
A           E           I 
A B C D E F G H I
Example-1:

When number of rows : 14
Even number of rows then two middle element(2 mid row + 2 mid column).

A B C D E F G H I J K L M N 
A                G H                N 
A                G H                N 
A                G H                N 
A                G H                N 
A                G H                N 
A B C D E F G H I J K L M N 
A B C D E F G H I J K L M N 
A                 G H               N 
A                 G H               N 
A                 G H               N 
A                 G H               N 
A                 G H               N 
A B C D E F G H I J K L M N

Now, let’s see the actual program to print it.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Approach to Print Windows Character Pattern:

  • Enter total row and store it in an integer variable row.
  • Calculate middle element.
    •  if n is odd we get 1 element .
    •  in case of n is even we get 2 values.
  • Take first for loop to print the row value and character for each row .
  • Take first inner for loop to print column value i.e., character  according to condition
    if (r == 1 || c == 1 || r == row || c == row) and if (r == a || c == a) and if (r == b || c == b)  else it will print space .
  • Then go on printing the character according to loop.

Java Code to Print Windows Character Pattern

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,a,b;
    // Starting ASCII value taken 64
    int asciiAlpha=64;
    //creating object of Scanner Class
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    // If n is odd then we will have only one middle element
    if (row % 2 != 0)
    {
      a = (row / 2) + 1;
      b = 0;
    }
    // If n is even then we will have two values
    else
    {
      a = (row / 2) + 1;
      b = row / 2 ;
    } 
    
    //outer for loop
    // to cover all the rows
    for(  r = 1; r <= row; r++)
    {
        //inner for loop 
        // to iterate all the column values
      for( c = 1; c <= row ; c++)
      {
 
        // If i,j equals to corner row  or column then print character
        if (r == 1 || c == 1 || r == row || c == row)
          System.out.print((char)(c+asciiAlpha)+" ");          
        else
        {
 
          // If i,j equals to the middle  row or column then  print character
          if (r == a || c == a)
            System.out.print((char)(c+asciiAlpha)+" ");
          else if (r == b || c == b)
            System.out.print((char)(c+asciiAlpha)+" ");
          else
            System.out.print("  ");
        }
      }
      System.out.println();
    }
  }
}
Output :

Case-1: Even number of rows taken.

Enter rows : 20

A B C D E F G H I J K L M N O P Q R S T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A B C D E F G H I J K L M N O P Q R S T 
A B C D E F G H I J K L M N O P Q R S T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A                         J K                             T 
A B C D E F G H I J K L M N O P Q R S T
Output :

Case-2: Odd number of rows taken.

Enter rows : 13

A B C D E F G H I J K L M 
A                 G              M 
A                 G              M 
A                 G              M 
A                 G              M 
A                 G              M 
A B C D E F G H I J K L M 
A                 G              M 
A                 G              M 
A                 G              M 
A                 G              M 
A                 G              M 
A B C D E F G H I J K L M

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Character Pattern Programs: