Java Program – Pattern 4

For n = 5

*
**
***
****
*****
****
***
**
*

Program

public class Pattern {

    public void print(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int i = n - 1; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

The given pattern can be split into two parts. The first part is :

*
**
***
****
*****

And the second part is

****
***
**
*

The first part is the same as this pattern. The second part is same as this pattern with n replaced with n-1.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>