Print rectangle pattern program in Java.





package demo;
public class RectanglePattern 
{
 public static void main(String[] args) 
 {
  int num = 4, q = 6;
  for (int r = 1; r <= num; r++, q = q - 2) 
  {
   for (int c = 1; c <= r; c++) {
    System.out.print(c);
   }
   for (int p = q; p >= 1; p--) {
    System.out.print(r);
   }
   for (int c = r - 1; c >= 1; c--) {
    System.out.print(c);
   }
   System.out.println();
  }
  for (int r = 3, m = 1; r >= 1; r--, m = m + 2) 
  {
   for (int c = 1; c <= r; c++) {
    System.out.print(c);
   }
   for (int n = m; n >= 1; n--) {
    System.out.print(r);
   }
   for (int c = r; c >= 1; c--) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}

Output:
run:
1111111
1222221
1233321
1234321
1233321
1222221
1111111

BUILD SUCCESSFUL (total time: 0 seconds)



No comments:

Post a Comment