how to print pyramid pattern in java ? Example

22:16 Unknown 0 Comments

Pattern based exercises are a better  way to understand  nested loops use in Java. There are many pattern based exercises and one of them is printing Pyramid Pattern code as shown below:
*  
* * 
* * * 
* * * * 
* * * * *
//program to print pyramid pattern in java

class Pattern1{
                public static void main(String[] jawed){
                                int i,j;
                                for(i=1;i<=5;i++){
                                                for(j=1;j<=i;j++){
                                                 System.out.print(i);      
                                                }
                                                System.out.println();
                                }
                }
}

Output:

Above given program , if print j then what will be the output:
Let’s see,


class Pattern1{
                public static void main(String[] jawed){
                                int i,j;
                                for(i=1;i<=5;i++){
                                                for(j=1;j<=i;j++){
                                                 System.out.print(j);      
                                                }
                                                System.out.println();
                                }
                }
}
Output:

If want print reverse Pattern then below show the code

class Pattern1_rev{
                public static void main(String[] jawed){
                                int i,j;
                                for(i=5;i>=1;i--){
                                                for(j=1;j<=i;j++){
                                                 System.out.print(i);      
                                                }
                                                System.out.println();
                                }
                }
}

Output:


That's all about how to print Pyramid using Java pattern.  Next differ pattern show in next post:
Hope you like this post
Plz share this!!! And have ou any query then write in comment.

0 comments:

e gift