Number rhombus pattern program in Java



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

Output:

Enter number of rows : 5
        1
      121
    12321
  1234321
123454321
  1234321
    12321
      121
        1
     

BUILD SUCCESSFUL (total time: 2 seconds)



No comments:

Post a Comment