Java Program to Print Diamond Pattern

By |2017-12-11T09:12:42+05:30June 23rd, 2016|java programs|

Here we are going to write a java program to print Diamond Pattern using numbers.


package com.onlinetutorialspoint.patterns;

import java.util.Scanner;

public class Diamond_Pattern {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter Size");
        int sizeOfDiamond = scanner.nextInt();
        int cols = 1;
        int startPoint = 0;
        int spaces = sizeOfDiamond / 2;
        for (int i = 1; i <= sizeOfDiamond; i++) {
            for (int k = 1; k <= spaces; k++) {
                System.out.print("");
            }
            if (i < sizeOfDiamond / 2 + 1) {
                startPoint = i;
                spaces = spaces - 1;
            } else {
                startPoint = (sizeOfDiamond + 1) - i;
                spaces = spaces + 1;
            }

            for (int j = 1; j <= cols; j++) {
                int midPoint = cols / 2 + 1;
                System.out.print(startPoint);
                if (j < midPoint) {
                    startPoint--;
                } else {
                    startPoint++;
                }
            }
            System.out.println();
            if (i < sizeOfDiamond / 2 + 1) {
                cols = cols + 2;
            } else {
                cols = cols - 2;
            }
        }
    }
}

Diamond Pattern

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment