Java Pyramid 6 - Java Language Basics

Java examples for Language Basics:for

Introduction

how to generate pyramid or triangle like given below using for loop.

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

Demo Code

 
public class Main {
 
        public static void main(String[] args) {
                //generate upper half of the pyramid
                for(int i=5; i>0 ;i--){
                        for(int j=0; j < i; j++){
                                System.out.print("*");
                        }/*w w w.  j av  a 2s .  c om*/
                       
                        //create a new line
                        System.out.println("");
                }
               
                //generate bottom half of the pyramid
                for(int i=1; i<= 5 ;i++){
                       
                        for(int j=0; j < i; j++){
                                System.out.print("*");
                        }
                       
                        //create a new line
                        System.out.println("");
                }
 
        }
}

Result


Related Tutorials