Number rectangle structure program in Java



import java.util.Scanner;
public class NumberRectangle
{
 public static void main(String[] args) 
 {
  int num, r = 1;
  System.out.print("Enter loop repeat number(rows): ");
  Scanner sc = new Scanner(System.in);
  num = sc.nextInt();
  System.out.println();
  for (; num >= 1; num--, r++) {
   for (int c = 1; c <= num; c++) {
    System.out.print(c);
   }
   for (int sp = r; sp > 1; sp--) {
    System.out.print("__");
   }
   for (int c = num; c >= 1; c--) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}

Output:

Enter loop repeat number(rows): 6

123456654321
12345__54321
1234____4321
123______321
12________21
1__________1

BUILD SUCCESSFUL (total time: 5 seconds)



Note: Hello friends, If you have any better solution for this programs then please comment below, I will add your comment as a solution for this programs.

Thanks for visiting my Blog.



No comments:

Post a Comment