Print Diamond Shaped Number Pattern | Java Programs

Output:

*** Diamond Number Pattern ***

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

Click Here For Java Online Compiler

Solution:

/**
 *
 * @author Alee Ahmed Kolachi
 */
public class DiamondNumberPattern {

    public static void main(String[] args) {

        int noOfRows = 6;
        int midRow = noOfRows / 2;
        int row = 1;

        System.out.println("*** Diamond Number Pattern ***" + "\n");

        //Printing upper half of the diamond
        for (int i = midRow; i > 0; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(" ");
            }

            for (int j = 1; j <= row; j++) {
                System.out.print(row + " ");
            }

            System.out.println();
            row++;
        }

        //Printing lower half of the diamond
        for (int i = 0; i <= midRow; i++) {

            for (int j = 1; j <= i; j++) {
                System.out.print(" ");
            }

            for (int j = row; j > 0; j--) {
                System.out.print(row + " ");
            }

            System.out.println();

            row--;
        }
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon