Java Diamond Pattern

In one of my previous post we have printed the Pyramid pattern. In this short tutorial we are going to print the Diamond pattern using java for loop.


public class StarDiamond {

    public static void main(String[] args) {

        int num = 5;
        for (int i = 1; i <= num; i++) {
            for (int j = num; j >= i; j--) {
                System.out.print(" ");
            }
            for (int m = 1; m <= i; m++) {
                System.out.print(" *");
            }
            System.out.print("\n");
        }
        for (int i = 1; i <= num; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(" ");
            }
            for (int m = num; m >= i; m--) {
                System.out.print(" *");
            }
            System.out.print("\n");
        }
    }
}


Output:




You may also like:

Pyramid Pattern







7 comments:

  1. this code was really help to me thanks

    ReplyDelete
  2. How to Write these pattern code
    *
    ** **
    *** ***
    ** **
    *


    How to Write this program to Print Star Pattern in Java

    ReplyDelete
  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here.
    Best Training Instittue

    ReplyDelete
  4. I have read your blog its very attractive and impressive. I like it your blog.
    Best Training and Real Time Support
    Android Training

    ReplyDelete