.
Previous Next

Write a Java program to print the numbers in diamond pattern?

/* Q. Write a Java Program to print the Numbers in Diamond pattern? */
public class NumbersInDiamond {
     public static void main(String[] args) {
           int rows = 9; /* give input here*/       
           for (int i = 1; i <= rows; i++) {
                int number = 1;
                System.out.format("%"+ (rows-i+1)*2+"s","");
                for (int j = 1; j <= i; j++) {
                     System.out.format("%4d",number++);   
                }
                System.out.println();
           }
           for (int i = rows-1; i >0 ; i--) {
                int number = 1;
                System.out.format("%"+ (rows-i+1)*2+"s","");
                for (int j = 1; j <= i; j++) {
                     System.out.format("%4d",number++);
                } 
                System.out.println();
           }         
      }
}
Previous Next