Sunday, June 28, 2015

Draw triangle of stars for the given number

Draw triangle with star/asterisk symbol and base of triangle is supplied as input

Input: 10

Output:
           *
          * *
         * * *
        * * * *
       * * * * *
      * * * * * *
     * * * * * * *
    * * * * * * * *
   * * * * * * * * *
  * * * * * * * * * *


Solution


public class StarTriangle {

private static void drawStarPyramid(int number) {

for(int i=1;i<=number;i++) {

for(int j=number;j>=i;j--) {
System.out.print(" ");
}

for(int k=1;k<=i;k++) {
System.out.print(" *");
}
System.out.println();

}
}

public static void main(String[] args) {

drawStarPyramid(10);

}


}

No comments:

Post a Comment