Java Program – Pattern 3

For n = 5

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

Program

public class Pattern {

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

Given the value n, this pattern contains n lines. Instead of numbering the lines as 1, 2, 3 … n, we number them as n, n-1, n-2, … , 3, 2, 1. When we number the lines in this way, line i contains i asterisks.

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>