Java examples for Language Basics:for
how to generate pyramid or triangle like given below using for loop.
* ** *** **** ***** ***** **** *** ** *
public class Main { public static void main(String[] args) { //from w ww.ja v a 2 s .c o m for(int i=1; i<= 5 ;i++){ for(int j=0; j < i; j++){ System.out.print("*"); } //generate a new line System.out.println(""); } //create second half of pyramid for(int i=5; i>0 ;i--){ for(int j=0; j < i; j++){ System.out.print("*"); } //generate a new line System.out.println(""); } } }