lundi 29 février 2016

How to print triangle pattern in java

Below are the snippet of the code. i want to print pattern like that.

1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1

Code

public class Pattern2 {

    public static void main(String[] args) {
        int p=1;
        for(int i=1;i<=4;i++){

            for(int j=1;j<=i;j++)
                System.out.print(""+p++);
            //p++;
            System.out.println("");

        }
       for(int i=4;i>=1;i--){
           //System.out.print("iii");
            int n=4;
            for(int j=1;j<=i;j++)

                System.out.print(p-n+j-1);
            n--;

            //n++;
            //System.out.println(p);
            System.out.println("");
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire