Java: Displaying an odd number right-triangle made up of characters -


i have write program takes size value , turns right triangle (made of # characters) such output looks like:

#      #   ### #####        #     ###   ##### ####### 

the first triangle of size 1. second triangle of size 4. third triangle of size 6. notice the number of characters per each line in given triangle odd. notice height of each triangle @ largest point can calculated size/2 + 1

i have attempted problem, stuck figuring out how can display correct number of # symbols per line.

here own code:

public class triangle {      public static void drawtriangle(int size) {         int column = (size/2) + 1;              (int = 0; <= column; i++) {                 (int j = size; j >= 0; j--) {                     if (i <= j) {                         system.out.print(" ");                     }                     else {                         system.out.print("#");                     }                 }                 system.out.println();             }     }      public static void main(string[] args) {         drawtriangle(1);         drawtriangle(4);         drawtriangle(6);     } } 

and here output code:

#      #    ##   ###        #      ##     ###    #### 

as can see have arranged triangles in proper way, , have gotten height of each triangle way supposed to. have no idea how can correct number off characters on each line...

i have tried several things including changing first loop from:

for (int = 0; <= column; i++) 

to

for (int = 1; <= column; i+=2) 

which filters out numbered rows, fails address other parameters such height of each triangle, , additional rows. here output changes:

 #     #   ###       #     ### 

any appreciated, thanks.

here go:

public static void drawtriangle(int size) {     (int = 1; <= size + 1; += 2) {         (int spaces = 0; spaces <= size - i; spaces++) {             system.out.print(" ");         }         (int hashes = 0; hashes < i; hashes++) {            system.out.print("#");         }         system.out.println();     }      system.out.println(); } 

as code-apprentice said, makes sense there 1 main for loop display each row in triangle. then, in order display correct number of spaces , hashes in each row, used 2 nested for loops.

update: simplified , improved code (using code-apprentice's suggestion)


Comments