Posts

Showing posts from April, 2019

Print equilateral triangle in java

Below prints Equilateral Triangle. public class PrintEqulateralTriangle {     public static void main(String[] args) {         int numberOfRow = 10;         int numberOfStars = numberOfRow + (numberOfRow - 1);         int median = (numberOfStars+1)/2;         for(int i=1; i<=numberOfRow; i++){             int start = (i==1 ? median : (median - i + 1));             int end = (i==1 ? median : (median + i - 1));             for(int j=1; j<=end;j++){                 if((j>= start) && (j <= end)){                 System.out.print("* ");                 }else{                 System.out.print("  ");                 }             }            System.out.println();         }     } } Output: --------                            *                         * * *                      * * * * *                   * * * * * * *                * * * * * * * * *             * * * * * * * * * * *          * * * * * * * * * * * * *  

Print Right Angle Triagle in Java

Below prints right angle triangle. public class PrintRightAngleTriangle {     public static void main(String[] args) {                for(int i=0; i<=10 ; i++){             for(int j=0;j<=i;j++){                 System.out.print("* ");             }             System.out.println();         }     } } Output: -------- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *