Number Rhombus Pattern Program in Java





package demo;
import java.util.Scanner;
public class NumberPattern
{
 public static void main(String[] args) 
 {
  int num, c;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter maximum number : ");
  num = sc.nextInt();
  for (int r = 1; r <= num; r++) {
   for (int sp = num - r; sp >= 1; sp--) {
    System.out.print(" ");
   }
   System.out.print(r);
   for (int sp = r * 2; sp > 1; sp--) {
    System.out.print(" ");
   }
   System.out.print(r);
   System.out.println();
  }
  for (int r = 1, n = num - 1; r < num; r++, n--) {
   for (int sp = r; sp >= 1; sp--) {
    System.out.print(" ");
   }
   System.out.print(n);
   for (int sp = n * 2; sp > 1; sp--) {
    System.out.print(" ");
   }
   System.out.print(n);
   System.out.println();
  }
 }
}

Output:

Enter maximum number : 5

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


BUILD SUCCESSFUL (total time: 3 seconds)



No comments:

Post a Comment