Java Program to Print Window Number Pattern

Program to Print Window Number Pattern

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

In this article we are going to see how to print window number program.

Example-1

Enter rows : 5

1 1 1 1 1 1 
2    2 2    2 
3 3 3 3 3 3 
4 4 4 4 4 4 
5    5 5    5 
6 6 6 6 6 6
Example-2: 

Enter rows : 5

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

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

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Approach to Print Window Number 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 number for each row .
    • Take first inner for loop to print column value i.e., number  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 numbers according to loop.

Java Code to Print Window Number 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;
    //creating object 
    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 ;
    } 
    for(  r = 1; r <= row; r++)
    {
      for( c = 1; c <= row ; c++)
      {
 
     
        if (r == 1 || c == 1 || r == row || c == row)
          System.out.print(r+" ");          
        else
        {
 
          
          if (r == a || c == a)
            System.out.print(r+" ");
          else if (r == b || c == b)
            System.out.print(r+" ");
          else
            System.out.print("  ");
        }
      }
      System.out.println();
    }
  }
}
Output:

CASE-1:
Enter rows : 6

1 1 1 1 1 1 
2    2 2    2 
3 3 3 3 3 3 
4 4 4 4 4 4 
5    5 5    5 
6 6 6 6 6 6
CASE-2:
Enter rows : 5

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

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 Number Pattern Programs: